/
db.go
395 lines (357 loc) · 9.78 KB
/
db.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package mongo
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"time"
"github.com/henrylee2cn/goutil"
"github.com/henrylee2cn/goutil/errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
tp "github.com/henrylee2cn/teleport"
"github.com/xiaoenai/tp-micro/model/redis"
)
// GO 1.9 alias
type M = bson.M
type Collection = mgo.Collection
type ObjectId = bson.ObjectId
var (
ErrNotFound = mgo.ErrNotFound
NewObjectId = bson.NewObjectId
)
// DB is a wrapper around sqlx.DB and redis.Client.
type DB struct {
*mgo.Session
Cache *redis.Client
mgoConfig *Config
redisConfig *redis.Config
cacheableDBs map[string]*CacheableDB
}
func (c *CacheableDB) getSession() (*mgo.Session, error) {
if c.DB.Session.Ping() != nil {
// Creating a maintenance socket pool for session
// connect to mongodb
session, err := mgo.DialWithInfo(c.DB.mgoConfig.Source())
if err != nil {
return nil, err
}
c.DB.Session = session
}
return c.DB.Session.Clone(), nil
}
// Cacheable the interface that can use cache.
// It must be orm-struct.
type Cacheable interface {
TableName() string
}
// CacheableDB cacheable DB handle
type CacheableDB struct {
*DB
tableName string
cachePriKeyPrefix string
cacheExpiration time.Duration
typeName string
module *redis.Module
}
// ErrCacheNil error: *DB.Cache (redis) is nil
var ErrCacheNil = errors.New("*DB.Cache (redis) is nil")
// RegCacheableDB registers a cacheable table.
func (d *DB) RegCacheableDB(ormStructPtr Cacheable, cacheExpiration time.Duration) (*CacheableDB, error) {
tableName := ormStructPtr.TableName()
if _, ok := d.cacheableDBs[tableName]; ok {
return nil, fmt.Errorf("re-register cacheable table: %s", tableName)
}
if !d.mgoConfig.NoCache && d.Cache == nil {
return nil, ErrCacheNil
}
module := redis.NewModule(d.mgoConfig.Database + ":" + tableName)
t := reflect.TypeOf(ormStructPtr)
c := &CacheableDB{
DB: d,
tableName: tableName,
// 采用固定前缀的二级缓存key
cachePriKeyPrefix: module.Key("_id"),
cacheExpiration: cacheExpiration,
typeName: t.String(),
module: module,
}
d.cacheableDBs[tableName] = c
return c, nil
}
// GetCacheableDB returns the specified *CacheableDB
func (d *DB) GetCacheableDB(tableName string) (*CacheableDB, error) {
c, ok := d.cacheableDBs[tableName]
if !ok {
return nil, fmt.Errorf("has not called *DB.RegCacheableDB() to register: %s", tableName)
}
return c, nil
}
// CacheKey cache key and values corresponding to primary keys
type CacheKey struct {
Key string
FieldValues []interface{}
isPriKey bool
}
var emptyCacheKey = CacheKey{}
// CreateCacheKeyByFields creates the cache key string by specified fields and values.
func (c *CacheableDB) CreateCacheKeyByFields(fields []string, values []interface{}) (string, error) {
if len(fields) != len(values) {
return "", errors.New("CreateCacheKeyByFields(): len(fields) != len(values)")
}
bs, err := json.Marshal(values)
if err != nil {
return "", errors.New("CreateCacheKeyByFields(): " + err.Error())
}
return c.module.Key(strings.Join(fields, "&") + goutil.BytesToString(bs)), nil
}
// CreateCacheKey creates cache key and fields' values.
// Note: if fields is empty, auto-use primary fields.
func (c *CacheableDB) CreateCacheKey(structPtr Cacheable, fields ...string) (CacheKey, error) {
var t = reflect.TypeOf(structPtr)
var typeName = t.String()
if c.typeName != typeName {
return emptyCacheKey, fmt.Errorf("CreateCacheKey(): unmatch Cacheable: want %s, have %s", c.typeName, typeName)
}
var v = reflect.ValueOf(structPtr).Elem()
var values = make([]interface{}, 0, 2)
var cacheKey string
if len(fields) == 0 {
// TODO 这里必须要传fields
return emptyCacheKey, errors.New("CreateCacheKey(): must transfer fields")
} else {
for i, field := range fields {
vv := v.FieldByName(goutil.CamelString(field))
if vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
values = append(values, vv.Interface())
// values = append(values, v.FieldByName(goutil.CamelString(field)).Interface())
fields[i] = goutil.SnakeString(field)
}
var err error
cacheKey, err = c.CreateCacheKeyByFields(fields, values)
if err != nil {
return emptyCacheKey, err
}
}
return CacheKey{
Key: cacheKey,
FieldValues: values,
}, nil
}
// CacheGet selects one row by primary key.
// Priority from the read cache.
// Note:
// If the cache does not exist, then write the cache;
// @destStructPtr must be a *struct type;
// If fields is empty, auto-use primary fields.
func (c *CacheableDB) CacheGet(destStructPtr Cacheable, fields ...string) error {
var cacheKey, err = c.CreateCacheKey(destStructPtr, fields...)
if err != nil {
return err
}
if c.DB.mgoConfig.NoCache {
// read db
return c.WitchCollection(func(collect *Collection) error {
return collect.Find(c.CreateGetQuery(cacheKey.FieldValues, fields...)).One(destStructPtr)
})
}
var (
key = cacheKey.Key
gettedFirstCacheKey = cacheKey.isPriKey
)
// read secondary cache
if !gettedFirstCacheKey {
var b []byte
b, err = c.Cache.Get(key).Bytes()
if err == nil {
key = goutil.BytesToString(b)
gettedFirstCacheKey = true
} else if !redis.IsRedisNil(err) {
return err
}
}
var exist bool
// get first cache
if gettedFirstCacheKey {
exist, err = c.getFirstCache(key, destStructPtr)
if err != nil {
return err
}
if exist {
// check
if !cacheKey.isPriKey && !c.checkSecondCache(destStructPtr, fields, cacheKey.FieldValues) {
c.Cache.Del(cacheKey.Key)
} else {
return nil
}
}
}
// to lock or get first cache
c.Cache.LockCallback("lock_"+key, func() {
var b []byte
FIRST:
if gettedFirstCacheKey {
exist, err = c.getFirstCache(key, destStructPtr)
if exist {
err = nil
return
}
if err != nil {
return
}
} else {
b, err = c.Cache.Get(key).Bytes()
if err == nil {
key = goutil.BytesToString(b)
gettedFirstCacheKey = true
goto FIRST
} else if !redis.IsRedisNil(err) {
return
}
}
err = c.WitchCollection(func(collect *Collection) error {
return collect.Find(c.CreateGetQuery(cacheKey.FieldValues, fields...)).One(destStructPtr)
})
if err != nil {
return
}
key, err = c.createPrikey(destStructPtr)
if err != nil {
tp.Errorf("CacheGet(): createPrikey: %s", err.Error())
err = nil
return
}
// write cache
data, _ := json.Marshal(destStructPtr)
err = c.Cache.Set(key, data, c.cacheExpiration).Err()
if err == nil && !cacheKey.isPriKey {
err = c.Cache.Set(cacheKey.Key, key, c.cacheExpiration).Err()
}
if err != nil {
tp.Errorf("CacheGet(): %s", err.Error())
err = nil
}
})
return err
}
func (c *CacheableDB) checkSecondCache(destStructPtr Cacheable, fields []string, values []interface{}) bool {
v := reflect.ValueOf(destStructPtr).Elem()
for i, field := range fields {
vv := v.FieldByName(goutil.CamelString(field))
if vv.Kind() == reflect.Ptr {
vv = vv.Elem()
}
if values[i] != vv.Interface() {
return false
}
}
return true
}
// get first cache
func (c *CacheableDB) getFirstCache(key string, destStructPtr Cacheable) (bool, error) {
data, err := c.Cache.Get(key).Bytes()
if err == nil {
err = json.Unmarshal(data, destStructPtr)
if err == nil {
return true, nil
}
tp.Errorf("CacheGet(): %s", err.Error())
} else if !redis.IsRedisNil(err) {
return false, err
}
return false, nil
}
// PutCache caches one row by primary key.
// Note:
// @destStructPtr must be a *struct type;
// If fields is empty, auto-use primary fields.
func (c *CacheableDB) PutCache(srcStructPtr Cacheable, fields ...string) error {
if c.DB.mgoConfig.NoCache {
return nil
}
cacheKey, err := c.CreateCacheKey(srcStructPtr, fields...)
if err != nil {
return err
}
data, err := json.Marshal(srcStructPtr)
if err != nil {
return err
}
key := cacheKey.Key
if cacheKey.isPriKey {
return c.Cache.Set(key, data, c.cacheExpiration).Err()
}
// secondary cache
key, err = c.createPrikey(srcStructPtr)
if err != nil {
return err
}
err = c.Cache.Set(key, data, c.cacheExpiration).Err()
if err != nil {
return err
}
return c.Cache.Set(cacheKey.Key, key, c.cacheExpiration).Err()
}
// DeleteCache deletes one row form cache by primary key.
// Note:
// @destStructPtr must be a *struct type;
// If fields is empty, auto-use primary fields.
func (c *CacheableDB) DeleteCache(srcStructPtr Cacheable, fields ...string) error {
if c.DB.mgoConfig.NoCache {
return nil
}
cacheKey, err := c.CreateCacheKey(srcStructPtr, fields...)
if err != nil {
return err
}
var keys = []string{cacheKey.Key}
// secondary cache
if !cacheKey.isPriKey {
// get first cache key
firstKey, err := c.Cache.Get(cacheKey.Key).Result()
if err == nil {
keys = append(keys, firstKey)
}
}
return c.Cache.Del(keys...).Err()
}
func (c *CacheableDB) createPrikey(structPtr Cacheable) (string, error) {
var v = reflect.ValueOf(structPtr).Elem()
// Parse objectIdHex's hex
objectIdHex := ObjectId(v.FieldByName("Id").String())
values := []interface{}{
objectIdHex.Hex(),
}
bs, err := json.Marshal(values)
if err != nil {
return "", errors.New("*CacheableDB.createPrikey(): " + err.Error())
}
return c.cachePriKeyPrefix + goutil.BytesToString(bs), nil
}
func (c *CacheableDB) CreateGetQuery(values []interface{}, whereFields ...string) M {
m := M{}
if len(whereFields) == 0 {
return m
}
for index, col := range whereFields {
m[col] = values[index]
}
return m
}
// Common method
func (c *CacheableDB) WitchCollection(s func(*Collection) error) error {
session, err := c.getSession()
if err != nil {
return fmt.Errorf("Mongodb connection error:%s", err)
}
defer func() {
session.Close()
if err := recover(); err != nil {
tp.Errorf("Mongodb close session err:%s", err)
}
}()
collection := c.DB.DB(c.DB.mgoConfig.Database).C(c.tableName)
return s(collection)
}