-
Notifications
You must be signed in to change notification settings - Fork 2
/
cacher.go
287 lines (259 loc) · 7.4 KB
/
cacher.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package gormcache
import (
"context"
"database/sql"
"encoding/json"
"errors"
"reflect"
"time"
"golang.org/x/sync/singleflight"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
)
const Name = "gorm-cache"
type nocachectx struct{}
// CacheKV cache the sql result.
type CacheKV interface {
Get(ctx context.Context, key string) (bool, string, error)
Set(ctx context.Context, key, value string, exp time.Duration) error
}
// Codec defines how to Marshal and Unmarshal cache data.
//
// the default Codec is std json.Marshal and json.Unmarshal.
type Codec interface {
Marshal(interface{}) ([]byte, error)
Unmarshal([]byte, interface{}) error
}
// ErrGormCache error for gorm-cache.
type ErrGormCache struct {
err error
}
func (e *ErrGormCache) Error() string { return Name + ":" + e.err.Error() }
// NewErrGormCache return ErrGormCache.
func NewErrGormCache(err error) error {
if err != nil {
return &ErrGormCache{err: err}
}
return nil
}
// GormCacher is cache plugin for gorm.io/gorm.
type GormCacher struct {
kv CacheKV
queryAfterRegisterName string
modelNames map[string]struct{}
exp time.Duration
requestGroup singleflight.Group
cacheKeyFunc func(*gorm.DB) string
codec Codec
}
// GormCache implements `gorm.Plugin` interface, exp is auto expires cache duration,
// Models is gorm model to be cached.
func GormCache(kv CacheKV, exp time.Duration, options ...Option) *GormCacher {
cacher := &GormCacher{
kv: kv,
queryAfterRegisterName: Name + ":after_query",
exp: exp,
cacheKeyFunc: func(db *gorm.DB) string {
return db.Dialector.Explain(db.Statement.SQL.String(), db.Statement.Vars...)
},
codec: &stdJsonCodec{},
}
for _, o := range options {
o(cacher)
}
return cacher
}
// Name `gorm.Plugin` implements.
func (g *GormCacher) Name() string { return Name }
// Initialize `gorm.Plugin` implements.
func (g *GormCacher) Initialize(db *gorm.DB) error {
if len(g.modelNames) == 0 {
return NewErrGormCache(errors.New("call `Models` to add model that needs to be cached"))
}
if err := db.Callback().Query().Replace("gorm:query", g.query); err != nil {
return NewErrGormCache(err)
}
if err := db.Callback().Query().After("gorm:query").Register(
g.queryAfterRegisterName,
g.queryAfter,
); err != nil {
return NewErrGormCache(err)
}
return nil
}
// query replace gorm:query
func (g *GormCacher) query(db *gorm.DB) {
if db.DryRun || db.Error != nil {
return
}
callbacks.BuildQuerySQL(db)
if g.canCache(db) {
cachekey := g.cacheKeyFunc(db)
ok, value, err := g.kv.Get(db.Statement.Context, cachekey)
if err != nil {
db.AddError(NewErrGormCache(err))
return
}
if ok {
g.unmarshalToDB(value, db)
return
}
g.queryFromDB(db, cachekey)
return
}
g.queryFromDB(db, "")
}
// queryFromDB query from database.
func (g *GormCacher) queryFromDB(db *gorm.DB, cachekey string) {
var (
rows *sql.Rows
err error
)
if cachekey == "" {
rows, err = db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
} else {
var any interface{}
any, err, _ = g.requestGroup.Do(cachekey, func() (interface{}, error) {
db.Statement.Context = context.WithValue(db.Statement.Context, nocachectx{}, 1)
return db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
})
rows = any.(*sql.Rows)
}
if err != nil {
db.AddError(err)
return
}
defer rows.Close()
gorm.Scan(rows, db, 0)
}
// queryAfter set cache to kv after query.
func (g *GormCacher) queryAfter(db *gorm.DB) {
if v := db.Statement.Context.Value(nocachectx{}); v == nil {
return
}
if db.Error != nil || db.Statement.Schema == nil || !g.canCache(db) {
return
}
ok, value, err := g.queryResult(db)
if !ok {
return
}
key := g.cacheKeyFunc(db)
if err != nil {
db.AddError(NewErrGormCache(err))
return
}
if err := g.kv.Set(db.Statement.Context, key, value, g.exp); err != nil {
db.AddError(NewErrGormCache(err))
return
}
}
// canCache checks whether the current sql is cacheable
func (g *GormCacher) canCache(db *gorm.DB) bool {
tname := structType(db.Statement.ReflectValue.Interface()).Name()
_, ok := g.modelNames[tname]
return ok
}
// queryResult get sql result as a string. only support struct or struct slice and array here.
// if not support, return false and empty string.
func (g *GormCacher) queryResult(db *gorm.DB) (bool, string, error) {
var (
fields = db.Statement.Schema.Fields
reflectValue = db.Statement.ReflectValue
)
switch reflectValue.Kind() {
case reflect.Struct:
valueData := make(map[string]interface{})
for _, field := range fields {
if fieldValue, isZero := field.ValueOf(db.Statement.Context, reflectValue); !isZero {
valueData[field.Name] = fieldValue
}
}
val, err := json.Marshal(valueData)
return true, string(val), err
case reflect.Slice, reflect.Array:
lens := reflectValue.Len()
valueArrData := make([]map[string]interface{}, lens)
for _, field := range fields {
for i := 0; i < lens; i++ {
if fieldValue, isZero := field.ValueOf(db.Statement.Context, reflectValue.Index(i)); !isZero {
if valueArrData[i] == nil {
valueArrData[i] = make(map[string]interface{})
}
valueArrData[i][field.Name] = fieldValue
}
}
}
val, err := json.Marshal(valueArrData)
return true, string(val), err
default:
db.Logger.Info(db.Statement.Context, "gorm-cache: %s, type not support", reflectValue.Kind().String())
}
return false, "", nil
}
// unmarshalToDB unmarshal cache value to gorm.DB, set data to dest.
func (g *GormCacher) unmarshalToDB(cacheValue string, db *gorm.DB) {
reflectValue := db.Statement.ReflectValue
err := json.Unmarshal([]byte(cacheValue), db.Statement.Dest)
if err != nil {
db.AddError(err)
return
}
elem := reflect.ValueOf(db.Statement.Dest)
if !elem.CanSet() {
elem = elem.Elem()
}
switch reflectValue.Kind() {
case reflect.Struct:
db.RowsAffected = 1
case reflect.Slice, reflect.Array:
db.RowsAffected = int64(elem.Len())
default:
db.Logger.Info(db.Statement.Context, "gorm-cache: %s, type not support", reflectValue.Kind().String())
}
reflectValue.Set(elem)
}
// structType get struct type name for checking whether the model can be cached.
func structType(val interface{}) reflect.Type {
typ := reflect.TypeOf(val)
for typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
var t reflect.Type
switch typ.Kind() {
case reflect.Array, reflect.Slice:
for typ.Elem().Kind() == reflect.Ptr {
typ = typ.Elem()
}
t = typ.Elem()
default:
t = typ
}
return t
}
type Option func(*GormCacher)
// CacheKeyFunc allow you diy cache key for CacheKV.
func CacheKeyFunc(f func(*gorm.DB) string) Option {
return func(gc *GormCacher) {
gc.cacheKeyFunc = f
}
}
// Models accept model that you want to be cached.
func Models(models ...interface{}) Option {
return func(gc *GormCacher) {
modelNames := make(map[string]struct{}, len(models))
for _, item := range models {
modelNames[structType(item).Name()] = struct{}{}
}
gc.modelNames = modelNames
}
}
// WithCodec inject your codec.
func WithCodec(codec Codec) Option {
return func(gc *GormCacher) {
gc.codec = codec
}
}
type stdJsonCodec struct{}
func (m *stdJsonCodec) Marshal(v interface{}) ([]byte, error) { return json.Marshal(v) }
func (m *stdJsonCodec) Unmarshal(data []byte, v interface{}) error { return json.Unmarshal(data, v) }