Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ func (c *ARC) GetIFPresent(key interface{}) (interface{}, error) {
return v, err
}

// GetOrSetFunc return the value and set if the key not found.
func (c *ARC) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
v, err := c.Get(key)
if err == KeyNotFoundError {
value, err := f()
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
return v, c.SetWithExpire(key, value, duration)
}
return v, nil
}

func (c *ARC) get(key interface{}, onLoad bool) (interface{}, error) {
v, err := c.getValue(key, onLoad)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Cache interface {
// GetIFPresent returns the value for the specified key if it is present in the cache.
// Return KeyNotFoundError if the key is not present.
GetIFPresent(key interface{}) (interface{}, error)
// GetOrSetFunc return the value and set if the key not found.
GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error)
// GetAll returns a map containing all key-value pairs in the cache.
GetALL(checkExpired bool) map[interface{}]interface{}
get(key interface{}, onLoad bool) (interface{}, error)
Expand Down
16 changes: 16 additions & 0 deletions lfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ func (c *LFUCache) GetIFPresent(key interface{}) (interface{}, error) {
return v, err
}

// GetOrSetFunc return the value and set if the key not found.
func (c *LFUCache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
v, err := c.Get(key)
if err == KeyNotFoundError {
value, err := f()
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
return v, c.SetWithExpire(key, value, duration)
}
return v, nil
}

func (c *LFUCache) get(key interface{}, onLoad bool) (interface{}, error) {
v, err := c.getValue(key, onLoad)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ func (c *LRUCache) GetIFPresent(key interface{}) (interface{}, error) {
return v, err
}

// GetOrSetFunc return the value and set if the key not found.
func (c *LRUCache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
v, err := c.Get(key)
if err == KeyNotFoundError {
value, err := f()
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
return v, c.SetWithExpire(key, value, duration)
}
return v, nil
}

func (c *LRUCache) get(key interface{}, onLoad bool) (interface{}, error) {
v, err := c.getValue(key, onLoad)
if err != nil {
Expand Down
16 changes: 16 additions & 0 deletions simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ func (c *SimpleCache) GetIFPresent(key interface{}) (interface{}, error) {
return v, nil
}

// GetOrSetFunc return the value and set if the key not found.
func (c *SimpleCache) GetOrSetFunc(key interface{}, f func() (interface{}, error), duration time.Duration) (interface{}, error) {
v, err := c.Get(key)
if err == KeyNotFoundError {
value, err := f()
if err != nil {
return nil, err
}
if value == nil {
return nil, nil
}
return v, c.SetWithExpire(key, value, duration)
}
return v, nil
}

func (c *SimpleCache) get(key interface{}, onLoad bool) (interface{}, error) {
v, err := c.getValue(key, onLoad)
if err != nil {
Expand Down