go.bigb.es/auxilia
Documentation
Index
- type Cache
- func New(defaultExpiration, cleanupInterval time.Duration) *Cache[K, V]
- func NewFromItems(defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Cache[K, V]
- func NewFromItemsWithPolicy(defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V], policy ExpirationPolicy) *Cache[K, V]
- func NewInitialized(defaultExpiration, cleanupInterval time.Duration) *Cache[K, V]
- func NewInitializedWithPolicy(defaultExpiration, cleanupInterval time.Duration, policy ExpirationPolicy) *Cache[K, V]
- func NewWithPolicy(defaultExpiration, cleanupInterval time.Duration, policy ExpirationPolicy) *Cache[K, V]
- func (*Cache[K, V]) Start(ctx context.Context)
- func (*Cache[K, V]) Stop()
- func (Cache) Add(k K, x V, d time.Duration) error
- func (Cache) Delete(k K)
- func (Cache) DeleteExpired()
- func (Cache) Flush()
- func (Cache) Get(k K) (V, bool)
- func (Cache) GetOrInsert(k K, insertV V, insertD time.Duration) (V, bool)
- func (Cache) GetWithExpiration(k K) (V, time.Time, bool)
- func (Cache) GetWithTouch(k K, d time.Duration) (V, bool)
- func (Cache) ItemCount() int
- func (Cache) ItemIterator() collect.ListIterator[collect.Tuple2[K, Item[V]]]
- func (Cache) ItemList() map[K]Item[V]
- func (Cache) OnDeletion(f func(K, V, DeletionCause))
- func (Cache) OnDeletionAppend(f func(K, V, DeletionCause))
- func (Cache) Replace(k K, x V, d time.Duration) error
- func (Cache) Set(k K, x V, d time.Duration)
- func (Cache) SetDefault(k K, x V)
- func (Cache) Touch(k K, d time.Duration) bool
- type DeletionCause
- type ExpirationPolicy
- type Item
Constants
const ( // NoExpiration indicates that an item should never expire. NoExpiration time.Duration = -2 // DefaultExpiration indicates that an item should use the cache's default expiration. DefaultExpiration time.Duration = -1 )
Variables
var ( // ErrNotExists is returned when an operation requires a key that is not present. ErrNotExists = errors.New("cache: record does not exist") // ErrAlreadyExists is returned when an insert finds the key already present. ErrAlreadyExists = errors.New("cache: record already exists") )
Types
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is the exported handle embedding *cache plus janitor.
func New[K comparable, V any](defaultExpiration, cleanupInterval time.Duration) *Cache[K, V]
New creates a new Cache with the given default expiration and cleanup interval. The background janitor is started immediately with context.Background.
func NewFromItems[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V]) *Cache[K, V]
NewFromItems creates a new Cache pre-seeded with items. The background janitor is started immediately with context.Background.
func NewFromItemsWithPolicy[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, items map[K]Item[V], policy ExpirationPolicy) *Cache[K, V]
NewFromItemsWithPolicy creates a new Cache pre-seeded with items and using the given expiration policy. The background janitor is started immediately with context.Background.
func NewInitialized[K comparable, V any](defaultExpiration, cleanupInterval time.Duration) *Cache[K, V]
NewInitialized creates a new Cache with the given default expiration and cleanup interval but does NOT start the background janitor. The caller must call Start to begin periodic expiry sweeping.
func NewInitializedWithPolicy[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, policy ExpirationPolicy) *Cache[K, V]
NewInitializedWithPolicy creates a new Cache with the given default expiration, cleanup interval, and expiration policy but does NOT start the background janitor. The caller must call Start to begin periodic expiry sweeping.
func NewWithPolicy[K comparable, V any](defaultExpiration, cleanupInterval time.Duration, policy ExpirationPolicy) *Cache[K, V]
NewWithPolicy creates a new Cache with the given default expiration, cleanup interval, and expiration policy. The background janitor is started immediately with context.Background.
func (c Cache) Add(k K, x V, d time.Duration) error
Add inserts x for k only if k is absent or expired. It returns ErrAlreadyExists if a live entry already exists.
func (c Cache) Delete(k K)
Delete removes the key k from the cache. If an onDeletion callback is registered, it is called after the lock is released.
func (c Cache) DeleteExpired()
DeleteExpired removes all expired items from the cache. If an onDeletion callback is registered, it is called for each evicted item after the lock is released.
func (c Cache) Get(k K) (V, bool)
Get returns the value for k if it exists and is not expired. The second return value indicates whether the key was found. Expired items are not deleted by Get.
func (c Cache) GetOrInsert(k K, insertV V, insertD time.Duration) (V, bool)
GetOrInsert atomically retrieves the value for k or inserts insertV if k is missing or expired. It returns the value and true if the key was already present and valid, or (insertV, false) if the value was inserted. Eviction callbacks fire after the lock is released.
GetWithExpiration returns the value and expiration time for k. The third return value indicates whether the key was found and unexpired. A zero expiration time means the item does not expire.
func (c Cache) GetWithTouch(k K, d time.Duration) (V, bool)
GetWithTouch atomically retrieves the value for k and refreshes its expiration. Returns (zero, false) if the key is missing or expired.
func (c Cache) ItemCount() int
ItemCount returns the number of items in the cache, including expired items that have not yet been swept.
func (c Cache) ItemIterator() collect.ListIterator[collect.Tuple2[K, Item[V]]]
ItemIterator returns a lazy iterator over all non-expired items in the cache. The iterator yields collect.Tuple2 pairs of (key, Item).
ItemList returns a snapshot of all non-expired items in the cache.
func (c Cache) OnDeletion(f func(K, V, DeletionCause))
OnDeletion sets the deletion callback. Deprecated: use OnDeletionAppend.
func (c Cache) OnDeletionAppend(f func(K, V, DeletionCause))
OnDeletionAppend prepends f in front of the existing callback chain. If f is nil the callback is disabled entirely.
func (c Cache) Replace(k K, x V, d time.Duration) error
Replace updates the value for k only if k is present and unexpired. It returns ErrNotExists if the key is missing or expired.
func (c Cache) Set(k K, x V, d time.Duration)
Set stores the value x for the key k with the given expiration duration.
func (c Cache) SetDefault(k K, x V)
SetDefault stores the value x for the key k using the cache's default expiration.
func (c *Cache[K, V]) Start(ctx context.Context)
Start starts the background janitor goroutine for periodic expiry sweeping.
func (c *Cache[K, V]) Stop()
Stop stops the background janitor goroutine.
func (c Cache) Touch(k K, d time.Duration) bool
Touch refreshes the expiration on an existing non-expired item. It returns true if the key was found and refreshed.
type DeletionCause int
DeletionCause indicates why an item was removed from the cache.
const ( // Evicted means the item was removed because it expired. Evicted DeletionCause // Deleted means the item was removed by an explicit Delete call. Deleted )
type ExpirationPolicy struct { // contains filtered or unexported fields }
ExpirationPolicy controls how the cache determines whether an item has expired. Use WriteTTL, SlidingTTL, or NewExpirationPolicy to create a policy.
func NewExpirationPolicy(fn func(expiration time.Time, lastAccess time.Time, ttl time.Duration) bool, touchOnRead bool) ExpirationPolicy
NewExpirationPolicy creates a custom ExpirationPolicy.
func SlidingTTL() ExpirationPolicy
SlidingTTL returns an ExpirationPolicy that resets the expiration window on every read (Get, GetWithExpiration, GetOrInsert).
func WriteTTL() ExpirationPolicy
WriteTTL returns an ExpirationPolicy that expires items based on their write-time deadline (the default behaviour).
type Item[V any] struct { Object V Expiration time.Time // absolute write-time deadline; zero means no expiration LastAccess time.Time // last access time; used by SlidingTTL TTL time.Duration // resolved TTL used to compute Expiration }
Item wraps a cached value with an absolute expiration timestamp.
Package cache provides a generic, TTL-based, in-memory key-value cache with background expiry sweeping and configurable expiration strategies.