/
handle_create.go
150 lines (138 loc) · 4.9 KB
/
handle_create.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package internal
import (
"encoding/json"
"fmt"
"time"
"github.com/go-redis/redis"
)
func newCreateHandle(ch *Handle) *createHandle {
create := &createHandle{handle: ch}
create.loadLua()
return create
}
type createHandle struct {
handle *Handle
setLuaSha string
}
// CreateModel
func (ch *createHandle) CreateModel(table string, primary interface{}, model interface{}, expiration int) (e error) {
var jsonModel JsonModel
jsonModel.PK = fmt.Sprint(primary)
jsonModel.Model = model
buffer, e := json.Marshal(jsonModel)
if e != nil {
return
}
key := ch.handle.JoinModelKey(table, jsonModel.PK)
e = ch.handle.redisClient.Set(key, string(buffer), time.Duration(expiration)*time.Second).Err()
ch.handle.Debug("Set model cache Key :", key, "value :", string(buffer), "error :", e)
return e
}
// CreateCountSearch
func (ch *createHandle) CreateCountSearch(table, key, field string, whereField []string, values []interface{}, expiration int, indexKeys []interface{}, joins ...struct {
ObjectField []string //使用的模型列
Table string //表名
}) (e error) {
field = ch.handle.JoinCountSecondKey(field)
return ch.CreateSearch(table, key, field, whereField, values, expiration, indexKeys, joins...)
}
// CreateSearch
//func (ch *createHandle) CreateSearch(table, key, field string, whereField []string, values []interface{}, expiration int, joins ...struct {
// ObjectField []string //使用的模型列
// Table string //表名
//}) (e error) {
//return ch.createLuaSearch(table, key, field, whereField, values,expiration,joins...)
//now := time.Now().Unix()
//jsearch := &JsonSearch{Timeout: now, Primarys: values}
//buff, e := json.Marshal(jsearch)
//if e != nil {
// return
//}
//
//searchKey := ch.handle.JoinSearchKey(table, key)
//e = ch.handle.redisClient.HSet(searchKey, field, buff).Err()
//ch.handle.Debug("Set search cache Key :", searchKey, "field :", field, "value :", string(buff), "error :", e)
//if e != nil {
// return
//}
//
//e = ch.handle.redisClient.Expire(searchKey, time.Duration(expiration)*time.Second).Err()
//if e != nil {
// return
//}
//
//for index := 0; index < len(whereField); index++ {
// affectKey := ch.handle.JoinAffectKey(table, whereField[index])
// e = ch.handle.redisClient.HSet(affectKey, searchKey, now).Err()
// ch.handle.Debug("Set affect cache Key :", affectKey, "field :", searchKey, "value :", now, "error :", e)
// if e != nil {
// return e
// }
//}
//for index := 0; index < len(joins); index++ {
// for j := 0; j < len(joins[index].ObjectField); j++ {
// affectKey := ch.handle.JoinAffectKey(joins[index].Table, joins[index].ObjectField[j])
// e = ch.handle.redisClient.HSet(affectKey, searchKey, now).Err()
// ch.handle.Debug("Set affect cache Key :", affectKey, "field :", searchKey, "value :", now, "error :", e)
// if e != nil {
// return e
// }
// }
//}
//return
//}
// CreateSearch
func (ch *createHandle) CreateSearch(table, key, field string, whereField []string, values []interface{}, expiration int, indexKeys []interface{}, joins ...struct {
ObjectField []string //使用的模型列
Table string //表名
}) (e error) {
timeout := time.Now().Unix() + int64(expiration)
var keys []string
var argv []interface{}
jsearch := &JsonSearch{Timeout: timeout, Primarys: values}
buff, e := json.Marshal(jsearch)
if e != nil {
return
}
searchKey := ch.handle.JoinSearchKey(table, key, indexKeys)
keys = append(keys, searchKey)
argv = append(argv, field, string(buff), expiration, expiration+3, timeout+3)
ch.handle.RefreshEvent(searchKey, true)
ch.handle.Debug("Add script set search cache key :", searchKey, "field :", field, "value :", string(buff), "error :", nil)
for index := 0; index < len(whereField); index++ {
affectKey := ch.handle.JoinAffectKey(table, whereField[index])
keys = append(keys, affectKey)
ch.handle.RefreshEvent(affectKey, false)
ch.handle.Debug("Add script set affect cache key :", affectKey, "field :", searchKey, "value :", timeout+3, "error :", nil)
}
for index := 0; index < len(joins); index++ {
for j := 0; j < len(joins[index].ObjectField); j++ {
affectKey := ch.handle.JoinAffectKey(joins[index].Table, joins[index].ObjectField[j])
keys = append(keys, affectKey)
ch.handle.RefreshEvent(affectKey, false)
ch.handle.Debug("Add script set affect cache key :", affectKey, "field :", searchKey, "value :", timeout+3, "error :", e)
}
}
_, e = ch.handle.redisClient.EvalSha(ch.setLuaSha, keys, argv...).Result()
ch.handle.Debug("Create script execution, keys :", keys, "value:", argv, "error :", e)
return e
}
func (ch *createHandle) loadLua() {
script := redis.NewScript(`
for k,v in pairs(KEYS) do
if k > 1 then
redis.call("HSET", v, KEYS[1], ARGV[5])
redis.call("EXPIRE", v, ARGV[4])
else
redis.call("HSET", v, ARGV[1], ARGV[2])
redis.call("EXPIRE", v, ARGV[3])
end
end
return true
`)
sha, err := script.Load(ch.handle.redisClient).Result()
if err != nil {
panic(err)
}
ch.setLuaSha = sha
}