Skip to content

Commit 880e506

Browse files
authored
Merge pull request #1 from sreedharani/sree_develop
(tag: v1.0.3) Ignore deleted entries in GetAll() flavors; Also add new GetActive() function
2 parents bc249e7 + 91cafc5 commit 880e506

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

cache.go

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ type LoadingCache interface {
5959
// to load value if it is not present.
6060
Get(Key) (Value, error)
6161

62+
GetActive(Key) (Value, error)
63+
6264
// Refresh loads new value for Key. If the Key already existed, the previous value
6365
// will continue to be returned by Get while the new value is loading.
6466
// If Key does not exist, this function will block until the value is loaded.

local.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache
22

33
import (
4+
"errors"
45
"sync"
56
"sync/atomic"
67
"time"
@@ -188,11 +189,25 @@ func (c *localCache) Get(k Key) (Value, error) {
188189
return en.getValue(), nil
189190
}
190191

192+
func (c *localCache) GetActive(k Key) (Value, error) {
193+
obj, err := c.Get(k)
194+
if err != nil {
195+
return nil, err
196+
}
197+
en := c.cache.get(k, sum(k))
198+
if ! en.getInvalidated() {
199+
return obj, nil
200+
}
201+
return nil, errors.New ("entry invalidated")
202+
}
203+
191204
// GetAllKeys returns all keys.
192205
func (c *localCache) GetAllKeys() []interface{} {
193206
keys := make([]interface{}, 0, c.cache.len())
194207
c.cache.walk(func(en *entry) {
195-
keys = append(keys, en.key)
208+
if ! en.getInvalidated() {
209+
keys = append(keys, en.key)
210+
}
196211
})
197212
return keys
198213
}
@@ -201,7 +216,9 @@ func (c *localCache) GetAllKeys() []interface{} {
201216
func (c *localCache) GetAllValues() []interface{} {
202217
values := make([]interface{}, 0, c.cache.len())
203218
c.cache.walk(func(en *entry) {
204-
values = append(values, en.getValue())
219+
if ! en.getInvalidated() {
220+
values = append(values, en.getValue())
221+
}
205222
})
206223
return values
207224
}
@@ -210,7 +227,9 @@ func (c *localCache) GetAllValues() []interface{} {
210227
func (c *localCache) GetAll() map[interface{}]interface{} {
211228
var values = make(map[interface{}]interface{}, c.cache.len())
212229
c.cache.walk(func(en *entry) {
213-
values[en.key] = en.getValue()
230+
if ! en.getInvalidated() {
231+
values[en.key] = en.getValue()
232+
}
214233
})
215234
return values
216235
}

0 commit comments

Comments
 (0)