Skip to content

Commit f265fe5

Browse files
committed
update implementation latest cache interface
1 parent 2a0b9c4 commit f265fe5

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

redis.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ func (p *Provider) Init(providerName string, appCfg *config.Config, logger log.L
6464

6565
p.client = redis.NewClient(p.clientOpts)
6666
if _, err := p.client.Ping().Result(); err != nil {
67-
return fmt.Errorf("aah/cache: %s", err)
67+
return fmt.Errorf("aah/cache/%s: %s", p.name, err)
6868
}
6969

7070
gob.Register(entry{})
71-
p.logger.Infof("Cache provider: %s connected successfully with %s", p.name, p.clientOpts.Addr)
71+
p.logger.Infof("aah/cache/provider: %s connected successfully with %s", p.name, p.clientOpts.Addr)
7272

7373
return nil
7474
}
@@ -111,30 +111,38 @@ func (r *redisCache) Get(k string) interface{} {
111111
k = r.keyPrefix + k
112112
v, err := r.p.client.Get(k).Bytes()
113113
if err != nil {
114+
if notacacheMiss(err) != nil {
115+
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k[len(r.keyPrefix):], err)
116+
}
114117
return nil
115118
}
116119

117120
var e entry
118121
err = gob.NewDecoder(bytes.NewBuffer(v)).Decode(&e)
119122
if err != nil {
123+
r.p.logger.Errorf("aah/cache/%s: %v", r.Name(), err)
120124
return nil
121125
}
122126
if r.p.cfg.EvictionMode == cache.EvictionModeSlide {
123-
_ = r.p.client.Expire(k, e.D)
127+
if err = r.p.client.Expire(k, e.D).Err(); err != nil {
128+
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k[len(r.keyPrefix):], err)
129+
}
124130
}
125131

126132
return e.V
127133
}
128134

129135
// GetOrPut method returns the cached entry for the given key if it exists otherwise
130136
// it puts the new entry into cache store and returns the value.
131-
func (r *redisCache) GetOrPut(k string, v interface{}, d time.Duration) interface{} {
137+
func (r *redisCache) GetOrPut(k string, v interface{}, d time.Duration) (interface{}, error) {
132138
ev := r.Get(k)
133139
if ev == nil {
134-
_ = r.Put(k, v, d)
135-
return v
140+
if err := r.Put(k, v, d); err != nil {
141+
return nil, err
142+
}
143+
return v, nil
136144
}
137-
return ev
145+
return ev, nil
138146
}
139147

140148
// Put method adds the cache entry with specified expiration. Returns error
@@ -144,7 +152,7 @@ func (r *redisCache) Put(k string, v interface{}, d time.Duration) error {
144152
buf := acquireBuffer()
145153
enc := gob.NewEncoder(buf)
146154
if err := enc.Encode(e); err != nil {
147-
return fmt.Errorf("aah/cache: %v", err)
155+
return fmt.Errorf("aah/cache/%s: %v", r.Name(), err)
148156
}
149157

150158
cmd := r.p.client.Set(r.keyPrefix+k, buf.Bytes(), d)
@@ -153,19 +161,29 @@ func (r *redisCache) Put(k string, v interface{}, d time.Duration) error {
153161
}
154162

155163
// Delete method deletes the cache entry from cache store.
156-
func (r *redisCache) Delete(k string) {
157-
r.p.client.Del(r.keyPrefix + k)
164+
func (r *redisCache) Delete(k string) error {
165+
if err := r.p.client.Del(r.keyPrefix + k).Err(); notacacheMiss(err) != nil {
166+
return fmt.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k, err)
167+
}
168+
return nil
158169
}
159170

160171
// Exists method checks given key exists in cache store and its not expried.
161172
func (r *redisCache) Exists(k string) bool {
162173
result, err := r.p.client.Exists(r.keyPrefix + k).Result()
163-
return err == nil && result == 1
174+
if err != nil {
175+
r.p.logger.Errorf("aah/cache/%s: key(%s) %v", r.Name(), k, err)
176+
return false
177+
}
178+
return result == 1
164179
}
165180

166181
// Flush methods flushes(deletes) all the cache entries from cache.
167-
func (r *redisCache) Flush() {
168-
r.p.client.FlushDB()
182+
func (r *redisCache) Flush() error {
183+
if err := r.p.client.FlushDB().Err(); err != nil {
184+
return fmt.Errorf("aah/cache/%s: %v", r.Name(), err)
185+
}
186+
return nil
169187
}
170188

171189
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
@@ -197,3 +215,10 @@ func parseDuration(v, f string) time.Duration {
197215
d, _ := time.ParseDuration(f)
198216
return d
199217
}
218+
219+
func notacacheMiss(err error) error {
220+
if err != nil && err.Error() == "redis: nil" {
221+
return nil
222+
}
223+
return err
224+
}

redis_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func TestRedisCache(t *testing.T) {
7474
}
7575

7676
err := c.Put("pre-test-key1", sample{Name: "Jeeva", Present: true, Value: "redis cache provider"}, 3*time.Second)
77-
assert.Equal(t, errors.New("aah/cache: gob: type not registered for interface: redis.sample"), err)
77+
assert.Equal(t, errors.New("aah/cache/cache1: gob: type not registered for interface: redis.sample"), err)
78+
_, _ = c.GetOrPut("pre-test-key1", sample{Name: "Jeeva", Present: true, Value: "redis cache provider"}, 3*time.Second)
7879

7980
gob.Register(map[string]interface{}{})
8081
gob.Register(sample{})
@@ -89,9 +90,11 @@ func TestRedisCache(t *testing.T) {
8990

9091
v := c.Get(tc.key)
9192
assert.Equal(t, tc.value, v)
93+
assert.True(t, c.Exists(tc.key))
9294

9395
c.Delete(tc.key)
94-
v = c.GetOrPut(tc.key, tc.value, 3*time.Second)
96+
v, err = c.GetOrPut(tc.key, tc.value, 3*time.Second)
97+
assert.Nil(t, err)
9598
assert.Equal(t, tc.value, v)
9699
})
97100
}
@@ -169,7 +172,8 @@ func TestRedisSlideEvictionMode(t *testing.T) {
169172
}
170173

171174
for i := 5; i < 10; i++ {
172-
v := c.GetOrPut(fmt.Sprintf("key_%v", i), i, 3*time.Second)
175+
v, err := c.GetOrPut(fmt.Sprintf("key_%v", i), i, 3*time.Second)
176+
assert.Nil(t, err)
173177
assert.Equal(t, i, v)
174178
}
175179

@@ -203,7 +207,7 @@ func TestRedisInvalidAddress(t *testing.T) {
203207
}`)
204208
l, _ := log.New(config.NewEmpty())
205209
err := mgr.InitProviders(cfg, l)
206-
assert.Equal(t, errors.New("aah/cache: dial tcp: address 637967: invalid port"), err)
210+
assert.Equal(t, errors.New("aah/cache/redis1: dial tcp: address 637967: invalid port"), err)
207211
}
208212

209213
func TestParseTimeDuration(t *testing.T) {

0 commit comments

Comments
 (0)