Skip to content

Commit

Permalink
Merge pull request #1 from sreedharani/sree_develop
Browse files Browse the repository at this point in the history
(tag: v1.0.3) Ignore deleted entries in GetAll() flavors; Also add new GetActive() function
  • Loading branch information
pt-nguyen committed Mar 27, 2024
2 parents bc249e7 + 91cafc5 commit 880e506
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 22 additions & 3 deletions local.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cache

import (
"errors"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 880e506

Please sign in to comment.