-
Notifications
You must be signed in to change notification settings - Fork 44
/
lua.go
75 lines (68 loc) · 1.49 KB
/
lua.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"flag"
"fmt"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-redis/redis/v8"
"os"
"time"
)
var (
addr string
password string
scriptBox = map[string]string{
"AddMailBoxSystemNotificationToCache": `
local key = KEYS[1]
local value = ARGV[1]
local uuid = ARGV[2]
local exist = redis.call("EXISTS", key)
if exist == 1 then
redis.call("LPUSH", key, value)
end
redis.call("HINCRBY", "message_system", uuid, 1)
return 0
`,
}
)
func NewRedis(logger log.Logger) redis.Cmdable {
l := log.NewHelper(log.With(logger, "lua", "redis"))
client := redis.NewClient(&redis.Options{
Addr: addr,
DB: 4,
DialTimeout: time.Second * 2,
PoolSize: 10,
Password: password,
})
timeout, cancelFunc := context.WithTimeout(context.Background(), time.Second*2)
defer cancelFunc()
err := client.Ping(timeout).Err()
if err != nil {
l.Fatalf("redis connect error: %v", err)
}
return client
}
func main() {
flag.Parse()
logger := log.NewStdLogger(os.Stdout)
r := NewRedis(logger)
ScriptLoad(r)
}
func ScriptLoad(r redis.Cmdable) {
for key, value := range scriptBox {
result, err := r.ScriptLoad(context.Background(), value).Result()
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(key, result)
}
}
func init() {
flag.StringVar(&addr, "addr",
"ip:port",
"redis addr, eg: -addr ip:port")
flag.StringVar(&password, "password",
"abc",
"redis password, eg: -password abc")
}