@@ -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.
161172func (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+ }
0 commit comments