1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package main
import (
"fmt"
"io/ioutil"
"time"
"github.com/gomodule/redigo/redis"
)
func CliPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 10,
MaxActive: 100,
IdleTimeout: time.Duration(20) * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
return nil, err
}
return c, nil
},
}
}
func main() {
var data []byte
var err error
if data, err = ioutil.ReadFile("test.lua"); err != nil {
fmt.Printf("read failed %v", err.Error())
return
}
script := redis.NewScript(1, string(data))
pool := CliPool()
defer pool.Close()
cli := pool.Get()
script.Load(cli)
arg := []string{"key", "value"}
args := redis.Args{}.AddFlat(arg)
var reply interface{}
if reply, err = script.Do(cli, args...); err != nil {
fmt.Printf("redis exec failed %v", err.Error())
return
}
fmt.Printf("return %v", reply)
}
|