Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExpirationLRUCache ¶
type ExpirationLRUCache[T any] struct { // contains filtered or unexported fields }
ExpirationLRUCache is an LRU cache with per-item expiration and optional callbacks. It is safe for concurrent use by multiple goroutines.
func NewCache ¶
func NewCache[T any](ctx context.Context, options Options) *ExpirationLRUCache[T]
NewCache creates a new ExpirationLRUCache with the given options. The cache is safe for concurrent use by multiple goroutines.
ctx: Context for controlling the lifetime of the background cleanup goroutine. options: Configuration for cache behavior.
func NewCacheWithOnExpired ¶
func NewCacheWithOnExpired[T any](ctx context.Context, options Options, onExpirationFn OnExpirationCallback[T], ) *ExpirationLRUCache[T]
NewCacheWithOnExpired creates a new ExpirationLRUCache with the given options and a custom expiration callback. The cache is safe for concurrent use by multiple goroutines.
ctx: Context for controlling the lifetime of the background cleanup goroutine. options: Configuration for cache behavior. onExpirationFn: Callback invoked before an item expires; can return a new value and TTL to keep the item alive.
func (*ExpirationLRUCache[T]) Clear ¶
func (e *ExpirationLRUCache[T]) Clear()
Clear removes all items from the cache.
func (*ExpirationLRUCache[T]) Get ¶
func (e *ExpirationLRUCache[T]) Get(key string) (val *T, ttl time.Duration)
Get retrieves a value from the cache by key. Can return already expired value. Returns the value pointer and remaining TTL if found, or (nil, 0) if not found.
key: The cache key.
func (*ExpirationLRUCache[T]) Put ¶
func (e *ExpirationLRUCache[T]) Put(key string, val *T, ttl time.Duration)
Put adds a value to the cache with the specified key and TTL (time-to-live). If ttl <= 0, the entry is not added.
key: The cache key. val: Pointer to the value to store. ttl: Duration before the item expires.
func (*ExpirationLRUCache[T]) TotalCount ¶
func (e *ExpirationLRUCache[T]) TotalCount() (count int)
TotalCount returns the current number of items in the cache.
type OnAfterPutCallback ¶
type OnAfterPutCallback func(newSize int)
OnAfterPutCallback will be called after put, receives new element count as parameter
type OnCacheHitCallback ¶
type OnCacheHitCallback func(key string)
OnCacheHitCallback will be called on cache get if entry was found
type OnCacheMissCallback ¶
type OnCacheMissCallback func(key string)
OnCacheMissCallback will be called on cache get and entry was not found
type OnExpirationCallback ¶
OnExpirationCallback will be called just before an element gets expired and will be removed from cache. This function can return new value and TTL to leave the element in the cache or nil to remove it
type Options ¶
type Options struct {
OnCacheHitFn OnCacheHitCallback
OnCacheMissFn OnCacheMissCallback
OnAfterPutFn OnAfterPutCallback
CleanupInterval time.Duration
MaxSize uint
}
Options configures the behavior of ExpirationLRUCache.
OnCacheHitFn: Optional callback invoked when a cache hit occurs. OnCacheMissFn: Optional callback invoked when a cache miss occurs. OnAfterPutFn: Optional callback invoked after a new item is put in the cache. CleanupInterval: How often expired items are cleaned up (default 10s). MaxSize: Maximum number of items in the cache (default 10,000).