Skip to content

Commit

Permalink
clarification
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Apr 10, 2024
1 parent 69ea787 commit 843440d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 4 additions & 0 deletions tools/walletextension/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import (
)

type Cache interface {
// EvictShortLiving - notify the cache that all short living elements cached before the events should be considered as evicted.
EvictShortLiving()

// IsEvicted - based on the eviction event and the time of caching, calculates whether the key was evicted
IsEvicted(key any, originalTTL time.Duration) bool

Set(key []byte, value any, ttl time.Duration) bool
Get(key []byte) (value any, ok bool)
Remove(key []byte)
Expand Down
25 changes: 13 additions & 12 deletions tools/walletextension/rpcapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,25 @@ func withCache[R any](cache cache.Cache, cfg *CacheCfg, cacheKey []byte, onCache
return onCacheMiss()
}

isEvictable := false
// we implement a custom cache eviction logic for the cache strategy of type LatestBatch.
// when a new batch is created, all entries with "LatestBatch" are considered evicted.
// elements not cached for a specific batch are not evicted
isEvicted := false
ttl := longCacheTTL
if cacheType == LatestBatch {
ttl = shortCacheTTL
isEvictable = true
isEvicted = cache.IsEvicted(cacheKey, ttl)
}

cachedValue, foundInCache := cache.Get(cacheKey)

// only entries cached with `LatestBatch` are evicted
isEvicted := isEvictable && cache.IsEvicted(cacheKey, ttl)

if foundInCache && !isEvicted {
returnValue, ok := cachedValue.(*R)
if !ok {
return nil, fmt.Errorf("unexpected error. Invalid format cached. %v", cachedValue)
if !isEvicted {
cachedValue, foundInCache := cache.Get(cacheKey)
if foundInCache {
returnValue, ok := cachedValue.(*R)
if !ok {
return nil, fmt.Errorf("unexpected error. Invalid format cached. %v", cachedValue)
}
return returnValue, nil
}
return returnValue, nil
}

result, err := onCacheMiss()
Expand Down

0 comments on commit 843440d

Please sign in to comment.