Documentation
¶
Overview ¶
Package cache Cache item implementation Based on https://github.com/ReneKroon/ttlcache
Index ¶
- Constants
- type Cache
- func (cache *Cache[K, T]) Close()
- func (cache *Cache[K, T]) Count() int
- func (cache *Cache[K, T]) Delete(key K)
- func (cache *Cache[K, T]) Get(key K) (T, bool)
- func (cache *Cache[K, T]) Load(key K) (T, bool)
- func (cache *Cache[K, T]) Purge()
- func (cache *Cache[K, T]) Range(cb func(k K, v T) bool)
- func (cache *Cache[K, T]) Remove(key K) bool
- func (cache *Cache[K, T]) Set(key K, data T)
- func (cache *Cache[K, T]) SetCheckExpirationCallback(callback checkExpireCallback[K, T])
- func (cache *Cache[K, T]) SetExpirationCallback(callback expireCallback[K, T])
- func (cache *Cache[K, T]) SetNewItemCallback(callback expireCallback[K, T])
- func (cache *Cache[K, T]) SetTTL(ttl time.Duration)
- func (cache *Cache[K, T]) SetWithTTL(key K, data T, ttl time.Duration)
- func (cache *Cache[K, T]) SetWithTTLOld(key K, data T, ttl time.Duration)
- func (cache *Cache[K, T]) SkipTtlExtensionOnHit(value bool)
- func (cache *Cache[K, T]) Store(key K, value T)
- func (cache *Cache[K, T]) StoreWithTTL(key K, value T, ttl time.Duration)
Constants ¶
const ( // ItemNotExpire is a special TTL value indicating that the item should not expire based on time. // However, it can still be removed from the cache through other means, such as manual deletion or callbacks. ItemNotExpire time.Duration = -1 // ItemExpireWithGlobalTTL is a special TTL value indicating that the item should use the cache's global TTL setting. ItemExpireWithGlobalTTL time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, T any] struct { // contains filtered or unexported fields }
Cache is a thread-safe, in-memory key/value store with support for time-to-live (TTL) expiration. It allows for both global and per-item TTL settings and provides callbacks for various events.
Type Parameters:
K: The type of the keys, which must be comparable. T: The type of the values stored in the cache.
func NewTtlCache ¶
func NewTtlCache[K comparable, T any]() *Cache[K, T]
NewTtlCache creates and returns a new Cache instance.
func (*Cache[K, T]) Close ¶
func (cache *Cache[K, T]) Close()
Close gracefully shuts down the cache, stopping the expiration processing goroutine and clearing all items. It is safe to call Close multiple times.
func (*Cache[K, T]) Get ¶
Get retrieves an item from the cache. It also extends the item's TTL unless disabled.
func (*Cache[K, T]) Purge ¶
func (cache *Cache[K, T]) Purge()
Purge removes all items from the cache.
func (*Cache[K, T]) Set ¶
func (cache *Cache[K, T]) Set(key K, data T)
Set adds or updates an item in the cache with the global TTL.
func (*Cache[K, T]) SetCheckExpirationCallback ¶
func (cache *Cache[K, T]) SetCheckExpirationCallback(callback checkExpireCallback[K, T])
SetCheckExpirationCallback sets a callback that allows for external validation before an item expires.
func (*Cache[K, T]) SetExpirationCallback ¶
func (cache *Cache[K, T]) SetExpirationCallback(callback expireCallback[K, T])
SetExpirationCallback sets the callback function to be executed when an item expires.
func (*Cache[K, T]) SetNewItemCallback ¶
func (cache *Cache[K, T]) SetNewItemCallback(callback expireCallback[K, T])
SetNewItemCallback sets the callback function to be executed when a new item is added to the cache.
func (*Cache[K, T]) SetWithTTL ¶
SetWithTTL adds or updates an item in the cache with a specific TTL.
func (*Cache[K, T]) SetWithTTLOld ¶ added in v1.2.181
SetWithTTLOld adds or updates an item in the cache with a specific TTL.
func (*Cache[K, T]) SkipTtlExtensionOnHit ¶
SkipTtlExtensionOnHit configures whether the TTL of an item is extended on a cache hit.
func (*Cache[K, T]) Store ¶
func (cache *Cache[K, T]) Store(key K, value T)
Store is an alias for Set.
func (*Cache[K, T]) StoreWithTTL ¶
StoreWithTTL is an alias for SetWithTTL.