Documentation
¶
Index ¶
- type Entry
- type ExpirationCache
- func (c *ExpirationCache[K, V]) All() iter.Seq2[K, V]
- func (c *ExpirationCache[K, V]) Backward() iter.Seq2[K, V]
- func (c *ExpirationCache[K, V]) Capacity() int
- func (c *ExpirationCache[K, V]) Clear()
- func (c *ExpirationCache[K, V]) Contains(key K) bool
- func (c *ExpirationCache[K, V]) Get(key K, now time.Time) (value V, ok bool)
- func (c *ExpirationCache[K, V]) GetEntry(key K, now time.Time) (entry *Entry[K, V], ok bool)
- func (c *ExpirationCache[K, V]) Len() int
- func (c *ExpirationCache[K, V]) Remove(key K) bool
- func (c *ExpirationCache[K, V]) SetFromHead(key K, value V, now, expiresAt time.Time)
- func (c *ExpirationCache[K, V]) SetFromTail(key K, value V, now, expiresAt time.Time)
- func (c *ExpirationCache[K, V]) TryContains(key K) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry[K comparable, V any] struct { Key K Value V }
Entry represents a key-value pair in the cache.
type ExpirationCache ¶
type ExpirationCache[K comparable, V any] struct { // contains filtered or unexported fields }
ExpirationCache is an in-memory cache with a fixed capacity where each entry has its own fixed expiration time. Expired entries are pruned on each set operation. When the cache reaches its capacity, insertions will evict the entry with the earliest expiration time (the head of the list).
func NewExpirationCache ¶
func NewExpirationCache[K comparable, V any](capacity int) *ExpirationCache[K, V]
NewExpirationCache returns a new expiration cache with the given capacity. If capacity is not positive, the cache will be effectively unbounded.
func (*ExpirationCache[K, V]) All ¶
func (c *ExpirationCache[K, V]) All() iter.Seq2[K, V]
All returns an iterator over all entries in the cache, starting from the earliest expiration (head) to the latest expiration (tail).
An ongoing iterator blocks concurrent writes until it completes.
func (*ExpirationCache[K, V]) Backward ¶
func (c *ExpirationCache[K, V]) Backward() iter.Seq2[K, V]
Backward returns an iterator over all entries in the cache, starting from the latest expiration (tail) to the earliest expiration (head).
An ongoing iterator blocks concurrent writes until it completes.
func (*ExpirationCache[K, V]) Capacity ¶
func (c *ExpirationCache[K, V]) Capacity() int
Capacity returns the maximum number of entries the cache can hold.
func (*ExpirationCache[K, V]) Clear ¶
func (c *ExpirationCache[K, V]) Clear()
Clear removes all entries from the cache.
func (*ExpirationCache[K, V]) Contains ¶
func (c *ExpirationCache[K, V]) Contains(key K) bool
Contains returns whether the cache contains the given key.
func (*ExpirationCache[K, V]) Get ¶
func (c *ExpirationCache[K, V]) Get(key K, now time.Time) (value V, ok bool)
Get returns the value associated with the given key, if it exists and is not expired.
func (*ExpirationCache[K, V]) GetEntry ¶
func (c *ExpirationCache[K, V]) GetEntry(key K, now time.Time) (entry *Entry[K, V], ok bool)
GetEntry returns the entry associated with the given key, if it exists and is not expired.
func (*ExpirationCache[K, V]) Len ¶
func (c *ExpirationCache[K, V]) Len() int
Len returns the number of entries in the cache.
func (*ExpirationCache[K, V]) Remove ¶
func (c *ExpirationCache[K, V]) Remove(key K) bool
Remove deletes the value associated with the given key and returns whether the key was found.
func (*ExpirationCache[K, V]) SetFromHead ¶
func (c *ExpirationCache[K, V]) SetFromHead(key K, value V, now, expiresAt time.Time)
SetFromHead inserts or updates the entry for the given key with the specified value and expiration time.
As opposed to SetFromTail, SetFromHead is optimized for cases where the new expiration time is expected to be among the earliest in the cache. It searches for the correct insertion point starting from the head of the list (the earliest expiration). If the key already exists, its node is updated and repositioned; otherwise, the oldest entry is evicted if at capacity. All expired entries are pruned before insertion.
Parameters:
- key: The key to insert or update.
- value: The value to associate with the key.
- now: The current time, used to prune expired entries.
- expiresAt: The expiration time for the entry.
func (*ExpirationCache[K, V]) SetFromTail ¶
func (c *ExpirationCache[K, V]) SetFromTail(key K, value V, now, expiresAt time.Time)
SetFromTail inserts or updates the entry for the given key with the specified value and expiration time.
As opposed to SetFromHead, SetFromTail is optimized for cases where the new expiration time is expected to be among the latest in the cache. It searches for the correct insertion point starting from the tail of the list (the latest expiration). If the key already exists, its node is updated and repositioned; otherwise, the oldest entry is evicted if at capacity. All expired entries are pruned before insertion.
Parameters:
- key: The key to insert or update.
- value: The value to associate with the key.
- now: The current time, used to prune expired entries.
- expiresAt: The expiration time for the entry.
func (*ExpirationCache[K, V]) TryContains ¶
func (c *ExpirationCache[K, V]) TryContains(key K) bool
TryContains is like Contains, but it immediately returns false if the cache is contended.