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]) 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 Will avoid the cachedItem being expired by TTL, but can still be exired by callback etc. ItemNotExpire time.Duration = -1 // ItemExpireWithGlobalTTL will use the global TTL when set. 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 synchronized map of items that can auto-expire once stale
func NewTtlCache ¶
func NewTtlCache[K comparable, T any]() *Cache[K, T]
NewTtlCache is a helper to create instance of the Cache struct
func (*Cache[K, T]) Close ¶
func (cache *Cache[K, T]) Close()
Close calls Purge, and then stops the goroutine that does ttl checking, for a clean shutdown. The cache is no longer cleaning up after the first call to Close, repeated calls are safe though.
func (*Cache[K, T]) Get ¶
Get is a thread-safe way to lookup items Every lookup, also touches the cachedItem, hence extending it's life
func (*Cache[K, T]) Set ¶
func (cache *Cache[K, T]) Set(key K, data T)
Set is a thread-safe way to add new items to the map
func (*Cache[K, T]) SetCheckExpirationCallback ¶
func (cache *Cache[K, T]) SetCheckExpirationCallback(callback checkExpireCallback[K, T])
SetCheckExpirationCallback sets a callback that will be called when an cachedItem is about to expire in order to allow external code to decide whether the cachedItem expires or remains for another TTL cycle
func (*Cache[K, T]) SetExpirationCallback ¶
func (cache *Cache[K, T]) SetExpirationCallback(callback expireCallback[K, T])
SetExpirationCallback sets a callback that will be called when an cachedItem expires
func (*Cache[K, T]) SetNewItemCallback ¶
func (cache *Cache[K, T]) SetNewItemCallback(callback expireCallback[K, T])
SetNewItemCallback sets a callback that will be called when a new cachedItem is added to the cache
func (*Cache[K, T]) SetWithTTL ¶
SetWithTTL is a thread-safe way to add new items to the map with individual ttl
func (*Cache[K, T]) SkipTtlExtensionOnHit ¶
SkipTtlExtensionOnHit allows the user to change the cache behaviour. When this flag is set to true it will no longer extend TTL of items when they are retrieved using Get, or when their expiration condition is evaluated using SetCheckExpirationCallback.
func (*Cache[K, T]) Store ¶
func (cache *Cache[K, T]) Store(key K, value T)
Store sets the key value.
func (*Cache[K, T]) StoreWithTTL ¶
StoreWithTTL sets the key value with TTL overrides the default.