forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_cmd_hook.go
135 lines (111 loc) · 2.75 KB
/
redis_cmd_hook.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gorr
import (
"bytes"
"encoding/binary"
"errors"
"github.com/go-redis/redis"
)
// command hook
func statusCmdValue(cmd *redis.StatusCmd) string {
value, err := getStoredValue(cmd.Args())
if err != nil {
return ""
}
return string(value)
}
func statusCmdResult(cmd *redis.StatusCmd) (string, error) {
value, err := getStoredValue(cmd.Args())
if err != nil {
return "", err
}
return string(value), nil
}
func stringCmdValue(cmd *redis.StringCmd) string {
value, err := getStoredValue(cmd.Args())
if err != nil {
return ""
}
return string(value)
}
func stringSliceCmdValue(cmd *redis.StringSliceCmd) []string {
ret, _ := stringSliceCmdResult(cmd)
return ret
}
func stringSliceCmdResult(cmd *redis.StringSliceCmd) ([]string, error) {
var ret []string
value, err1 := getStoredValue(cmd.Args())
if err1 != nil {
return ret, errors.New("get value from db failed")
}
var buff bytes.Buffer
sz, err2 := buff.Write(value)
if err2 != nil || sz != len(value) {
return ret, errors.New("write to buffer failed")
}
var slen int32
err3 := binary.Read(&buff, binary.LittleEndian, &slen)
if err3 != nil {
return ret, errors.New("read size of slice from buffer failed")
}
sz -= 4
for i := 0; i < int(slen); i++ {
var sz2 int32
err := binary.Read(&buff, binary.LittleEndian, &sz2)
if err != nil || sz2 < 0 || int(sz2) > sz-4 {
return ret, errors.New("read string size from buffer failed")
}
sz -= 4
data := make([]byte, sz2)
err = binary.Read(&buff, binary.LittleEndian, data)
if err != nil {
return ret, errors.New("read string from buffer failed")
}
sz -= int(sz2)
ret = append(ret, string(data))
}
return ret, nil
}
func stringCmdResult(cmd *redis.StringCmd) (string, error) {
value, err := getStoredValue(cmd.Args())
if err != nil {
return "", err
}
return string(value), nil
}
func intCmdValue(cmd *redis.IntCmd) int64 {
var buff bytes.Buffer
value, err1 := getStoredValue(cmd.Args())
if err1 != nil {
// fmt.Printf("read int cmd from db failed, error:%s\n", err1.Error())
return 0
}
sz, err2 := buff.Write(value)
if err2 != nil || sz != len(value) {
// fmt.Printf("read int cmd size failed, error:%s, sz:%d, len(value):%d\n", err2.Error(), sz, len(value))
return 0.0
}
var ret int64
err := binary.Read(&buff, binary.LittleEndian, &ret)
if err != nil {
// fmt.Printf("read int cmd failed, error:%s\n", err.Error())
return 0
}
return ret
}
func floatCmdValue(cmd *redis.FloatCmd) float64 {
var buff bytes.Buffer
value, err1 := getStoredValue(cmd.Args())
if err1 != nil {
return 0.0
}
sz, err2 := buff.Write(value)
if err2 != nil || sz != len(value) {
return 0.0
}
var ret float64
err := binary.Read(&buff, binary.LittleEndian, &ret)
if err != nil {
return 0.0
}
return ret
}