|
| 1 | +// Package cache provides a time cache implementation safe for concurrent use |
| 2 | +// without the need of additional locking. |
| 3 | +package cache |
| 4 | + |
| 5 | +import ( |
| 6 | + "container/list" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type genericCacheEntry[T any] struct { |
| 12 | + value T |
| 13 | + timestamp time.Time |
| 14 | +} |
| 15 | + |
| 16 | +// GenericTimeCache provides a generic time cache safe for concurrent use by |
| 17 | +// multiple goroutines without additional locking or coordination. |
| 18 | +// The implementation is based on the simple TimeCache. |
| 19 | +type GenericTimeCache[T any] struct { |
| 20 | + // all keys in the cache in the order they were added |
| 21 | + // most recent keys are on the front of the indexer; |
| 22 | + // it is used to optimize cache sweeping |
| 23 | + indexer *list.List |
| 24 | + // key in the cache with the value and timestamp it's been added |
| 25 | + // to the cache the last time |
| 26 | + cache map[string]*genericCacheEntry[T] |
| 27 | + // the timespan after which entry in the cache is considered |
| 28 | + // as outdated and can be removed from the cache |
| 29 | + timespan time.Duration |
| 30 | + mutex sync.RWMutex |
| 31 | +} |
| 32 | + |
| 33 | +// NewGenericTimeCache creates a new generic cache instance with provided timespan. |
| 34 | +func NewGenericTimeCache[T any](timespan time.Duration) *GenericTimeCache[T] { |
| 35 | + return &GenericTimeCache[T]{ |
| 36 | + indexer: list.New(), |
| 37 | + cache: make(map[string]*genericCacheEntry[T]), |
| 38 | + timespan: timespan, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// Add adds an entry to the cache. Returns `true` if entry was not present in |
| 43 | +// the cache and was successfully added into it. Returns `false` if |
| 44 | +// entry is already in the cache. This method is synchronized. |
| 45 | +func (tc *GenericTimeCache[T]) Add(key string, value T) bool { |
| 46 | + tc.mutex.Lock() |
| 47 | + defer tc.mutex.Unlock() |
| 48 | + |
| 49 | + _, ok := tc.cache[key] |
| 50 | + if ok { |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + tc.sweep() |
| 55 | + |
| 56 | + tc.cache[key] = &genericCacheEntry[T]{ |
| 57 | + value: value, |
| 58 | + timestamp: time.Now(), |
| 59 | + } |
| 60 | + tc.indexer.PushFront(key) |
| 61 | + return true |
| 62 | +} |
| 63 | + |
| 64 | +// Get gets an entry from the cache. Boolean flag is `true` if entry is |
| 65 | +// present and `false` otherwise. |
| 66 | +func (tc *GenericTimeCache[T]) Get(key string) (T, bool) { |
| 67 | + tc.mutex.RLock() |
| 68 | + defer tc.mutex.RUnlock() |
| 69 | + |
| 70 | + entry, ok := tc.cache[key] |
| 71 | + if !ok { |
| 72 | + var zeroValue T |
| 73 | + return zeroValue, ok |
| 74 | + } |
| 75 | + |
| 76 | + return entry.value, ok |
| 77 | +} |
| 78 | + |
| 79 | +// Sweep removes old entries. That is those for which caching timespan has |
| 80 | +// passed. |
| 81 | +func (tc *GenericTimeCache[T]) Sweep() { |
| 82 | + tc.mutex.Lock() |
| 83 | + defer tc.mutex.Unlock() |
| 84 | + |
| 85 | + tc.sweep() |
| 86 | +} |
| 87 | + |
| 88 | +func (tc *GenericTimeCache[T]) sweep() { |
| 89 | + for { |
| 90 | + back := tc.indexer.Back() |
| 91 | + if back == nil { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + key := back.Value.(string) |
| 96 | + entry, ok := tc.cache[key] |
| 97 | + if !ok { |
| 98 | + logger.Errorf( |
| 99 | + "inconsistent cache state - expected key [%v] is not present", |
| 100 | + key, |
| 101 | + ) |
| 102 | + break |
| 103 | + } |
| 104 | + |
| 105 | + if time.Since(entry.timestamp) > tc.timespan { |
| 106 | + tc.indexer.Remove(back) |
| 107 | + delete(tc.cache, key) |
| 108 | + } else { |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments