-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
166 lines (152 loc) · 4.19 KB
/
storage.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
package storage
import (
"reflect"
log "github.com/golang/glog"
"github.com/1024casts/go-common/context"
)
type Storage interface {
Get(ctx *context.Context, key Key, value interface{}) error
Set(ctx *context.Context, key Key, object interface{}) error
Add(ctx *context.Context, key Key, object interface{}) error
MultiGet(ctx *context.Context, keys []Key, valuesMap interface{}) error
MultiSet(ctx *context.Context, values map[Key]interface{}) error
Delete(ctx *context.Context, key ...Key) error
}
type StorageProxy struct {
PreferedStorage Storage
BackupStorage Storage
}
func NewStorageProxy(prefered, backup Storage) *StorageProxy {
return &StorageProxy{
PreferedStorage: prefered,
BackupStorage: backup,
}
}
func (this *StorageProxy) Get(ctx *context.Context, key Key, value interface{}) error {
err := this.PreferedStorage.Get(ctx, key, value)
if err != nil && reflect.TypeOf(err).Name() == "EmptyObjectError" {
err = this.BackupStorage.Get(ctx, key, value)
if err != nil {
return err
}
err = this.PreferedStorage.Set(ctx, key, value)
if err != nil {
return err
}
}
if err != nil {
return err
}
return nil
}
func (this *StorageProxy) Add(ctx *context.Context, key Key, object interface{}) error {
// 这段代码先BackupStorage后PreferedStorage
// 因为数据库有auto increment 的情况,add时,key无意义
// 需要插入成功后再来获取key
if object != nil {
err := this.BackupStorage.Add(ctx, key, object)
if err != nil {
return err
}
keyChangeableObj, iskeyChangeableI := object.(KeyChangeable)
keyGetterObj, isKeyGetterI := object.(KeyGetter)
if iskeyChangeableI && isKeyGetterI {
if keyChangeableObj.IsKeyChangeable() {
key = keyGetterObj.GetKey()
}
}
err = this.PreferedStorage.Add(ctx, key, object)
if err != nil {
return err
}
}
return nil
}
func (this *StorageProxy) Set(ctx *context.Context, key Key, object interface{}) error {
if object != nil {
err := this.PreferedStorage.Set(ctx, key, object)
if err != nil {
return err
}
err = this.BackupStorage.Set(ctx, key, object)
if err != nil {
return err
}
}
return nil
}
func (this *StorageProxy) MultiGet(ctx *context.Context, keys []Key, valuesMap interface{}) error {
err := this.PreferedStorage.MultiGet(ctx, keys, valuesMap)
if err != nil {
log.Warning(err)
return nil
}
missedKeyCount := 0
valueMapReflect := reflect.ValueOf(valuesMap)
missedKeys := make([]Key, 0, missedKeyCount)
for _, key := range keys {
if !valueMapReflect.MapIndex(reflect.ValueOf(key)).IsValid() {
missedKeyCount++
missedKeys = append(missedKeys, key)
}
}
if missedKeyCount > 0 {
missedMap := make(map[Key]interface{})
err := this.BackupStorage.MultiGet(ctx, missedKeys, missedMap)
if err != nil {
return err
}
if len(missedMap) == 0 {
return nil
}
this.PreferedStorage.MultiSet(ctx, missedMap)
for k, v := range missedMap {
valueMapReflect.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
}
return nil
}
func (this *StorageProxy) MultiSet(ctx *context.Context, objectMap map[Key]interface{}) error {
err := this.PreferedStorage.MultiSet(ctx, objectMap)
if err != nil {
return err
}
err = this.BackupStorage.MultiSet(ctx, objectMap)
if err != nil {
return err
}
return nil
}
func (this *StorageProxy) Delete(ctx *context.Context, key ...Key) error {
err := this.BackupStorage.Delete(ctx, key...)
if err != nil {
return err
}
err = this.PreferedStorage.Delete(ctx, key...)
if err != nil {
return err
}
return nil
}
func (this *StorageProxy) Incr(ctx *context.Context, key Key, step int64) (newValue int64, err error) {
result, err := this.PreferedStorage.(CounterStorage).Incr(ctx, key, step)
if err != nil {
return result, err
}
result, err = this.BackupStorage.(CounterStorage).Incr(ctx, key, step)
if err != nil {
return result, err
}
return result, err
}
func (this *StorageProxy) Decr(ctx *context.Context, key Key, step int64) (newValue int64, err error) {
result, err := this.PreferedStorage.(CounterStorage).Decr(ctx, key, step)
if err != nil {
return result, err
}
result, err = this.BackupStorage.(CounterStorage).Decr(ctx, key, step)
if err != nil {
return result, err
}
return result, err
}