From 91cafc50e7c4475911bbc56ecfd6015c136a3a1b Mon Sep 17 00:00:00 2001 From: Sree Oggu Date: Tue, 26 Mar 2024 23:47:50 -0700 Subject: [PATCH] (tag: v1.0.3) Ignore deleted entries in GetAll() functions. Also add new GetActive() --- cache.go | 2 ++ local.go | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index ea5129b..73f094c 100644 --- a/cache.go +++ b/cache.go @@ -59,6 +59,8 @@ type LoadingCache interface { // to load value if it is not present. Get(Key) (Value, error) + GetActive(Key) (Value, error) + // Refresh loads new value for Key. If the Key already existed, the previous value // will continue to be returned by Get while the new value is loading. // If Key does not exist, this function will block until the value is loaded. diff --git a/local.go b/local.go index aae8aa4..c9f9447 100644 --- a/local.go +++ b/local.go @@ -1,6 +1,7 @@ package cache import ( + "errors" "sync" "sync/atomic" "time" @@ -188,11 +189,25 @@ func (c *localCache) Get(k Key) (Value, error) { return en.getValue(), nil } +func (c *localCache) GetActive(k Key) (Value, error) { + obj, err := c.Get(k) + if err != nil { + return nil, err + } + en := c.cache.get(k, sum(k)) + if ! en.getInvalidated() { + return obj, nil + } + return nil, errors.New ("entry invalidated") +} + // GetAllKeys returns all keys. func (c *localCache) GetAllKeys() []interface{} { keys := make([]interface{}, 0, c.cache.len()) c.cache.walk(func(en *entry) { - keys = append(keys, en.key) + if ! en.getInvalidated() { + keys = append(keys, en.key) + } }) return keys } @@ -201,7 +216,9 @@ func (c *localCache) GetAllKeys() []interface{} { func (c *localCache) GetAllValues() []interface{} { values := make([]interface{}, 0, c.cache.len()) c.cache.walk(func(en *entry) { - values = append(values, en.getValue()) + if ! en.getInvalidated() { + values = append(values, en.getValue()) + } }) return values } @@ -210,7 +227,9 @@ func (c *localCache) GetAllValues() []interface{} { func (c *localCache) GetAll() map[interface{}]interface{} { var values = make(map[interface{}]interface{}, c.cache.len()) c.cache.walk(func(en *entry) { - values[en.key] = en.getValue() + if ! en.getInvalidated() { + values[en.key] = en.getValue() + } }) return values }