/
cache.go
168 lines (136 loc) · 4.58 KB
/
cache.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
package meta
import (
"errors"
"github.com/opensds/multi-cloud/s3/pkg/helper"
"github.com/opensds/multi-cloud/s3/pkg/meta/redis"
log "github.com/sirupsen/logrus"
)
type CacheType int
const (
NoCache CacheType = iota
EnableCache
SimpleCache
)
const (
MSG_NOT_IMPL = "not implemented."
)
var cacheNames = [...]string{"NOCACHE", "EnableCache", "SimpleCache"}
type MetaCache interface {
Close()
Get(table redis.RedisDatabase, prefix, key string,
onCacheMiss func() (helper.Serializable, error),
onDeserialize func(map[string]string) (interface{}, error),
willNeed bool) (value interface{}, err error)
Remove(table redis.RedisDatabase, prefix, key string)
GetCacheHitRatio() float64
Keys(table redis.RedisDatabase, pattern string) ([]string, error)
HGetInt64(table redis.RedisDatabase, prefix, key, field string) (int64, error)
HMSet(table redis.RedisDatabase, prefix, key string, fields map[string]interface{}) (string, error)
HIncrBy(table redis.RedisDatabase, prefix, key, field string, value int64) (int64, error)
}
type disabledMetaCache struct{}
type entry struct {
table redis.RedisDatabase
key string
value interface{}
}
func newMetaCache(myType CacheType) (m MetaCache) {
log.Infof("Setting Up Metadata Cache: %s\n", cacheNames[int(myType)])
if myType == SimpleCache {
m := new(enabledSimpleMetaCache)
m.Hit = 0
m.Miss = 0
return m
}
return &disabledMetaCache{}
}
func (m *disabledMetaCache) Get(table redis.RedisDatabase, prefix, key string,
onCacheMiss func() (helper.Serializable, error),
onDeserialize func(map[string]string) (interface{}, error),
willNeed bool) (value interface{}, err error) {
return onCacheMiss()
}
func (m *disabledMetaCache) Remove(table redis.RedisDatabase, prefix, key string) {
return
}
func (m *disabledMetaCache) GetCacheHitRatio() float64 {
return -1
}
func (m *disabledMetaCache) Keys(table redis.RedisDatabase, pattern string) ([]string, error) {
return nil, errors.New(MSG_NOT_IMPL)
}
func (m *disabledMetaCache) HGetInt64(table redis.RedisDatabase, prefix, key, field string) (int64, error) {
return 0, errors.New(MSG_NOT_IMPL)
}
func (m *disabledMetaCache) HMSet(table redis.RedisDatabase, prefix, key string, fields map[string]interface{}) (string, error) {
return "", errors.New(MSG_NOT_IMPL)
}
func (m *disabledMetaCache) HIncrBy(table redis.RedisDatabase, prefix, key, field string, value int64) (int64, error) {
return 0, errors.New(MSG_NOT_IMPL)
}
func (m *disabledMetaCache) Close() {
}
type enabledSimpleMetaCache struct {
Hit int64
Miss int64
}
func (m *enabledSimpleMetaCache) Get(
table redis.RedisDatabase,
prefix, key string,
onCacheMiss func() (helper.Serializable, error),
onDeserialize func(map[string]string) (interface{}, error),
willNeed bool) (value interface{}, err error) {
log.Info("enabledSimpleMetaCache Get. table:", table, "key:", key)
fields, err := redis.HGetAll(table, prefix, key)
if err != nil {
log.Error("enabledSimpleMetaCache Get err:", err, "table:", table, "key:", key)
}
if err == nil && fields != nil && len(fields) > 0 {
value, err = onDeserialize(fields)
m.Hit = m.Hit + 1
return value, err
}
//if redis doesn't have the entry
if onCacheMiss != nil {
obj, err := onCacheMiss()
if err != nil {
return nil, err
}
if willNeed == true {
values, err := obj.Serialize()
if err != nil {
log.Error("failed to serialize from %v", obj, " with err: ", err)
return nil, err
}
_, err = redis.HMSet(table, prefix, key, values)
if err != nil {
log.Error("failed to set key: ", key, " with err: ", err)
//do nothing, even if redis is down.
}
}
m.Miss = m.Miss + 1
return obj, nil
}
return nil, nil
}
func (m *enabledSimpleMetaCache) Remove(table redis.RedisDatabase, prefix, key string) {
redis.Remove(table, prefix, key)
}
func (m *enabledSimpleMetaCache) GetCacheHitRatio() float64 {
return float64(m.Hit) / float64(m.Hit+m.Miss)
}
func (m *enabledSimpleMetaCache) Keys(table redis.RedisDatabase, pattern string) ([]string, error) {
return redis.Keys(table, pattern)
}
func (m *enabledSimpleMetaCache) HGetInt64(table redis.RedisDatabase, prefix, key, field string) (int64, error) {
return redis.HGetInt64(table, prefix, key, field)
}
func (m *enabledSimpleMetaCache) HMSet(table redis.RedisDatabase, prefix, key string, fields map[string]interface{}) (string, error) {
return redis.HMSet(table, prefix, key, fields)
}
func (m *enabledSimpleMetaCache) HIncrBy(table redis.RedisDatabase, prefix, key, field string, value int64) (int64, error) {
return redis.HIncrBy(table, prefix, key, field, value)
}
func (m *enabledSimpleMetaCache) Close() {
redis.Close()
}