Documentation ¶
Overview ¶
Package cache Cache item implementation Based on https://github.com/ReneKroon/ttlcache
Index ¶
- Constants
- type Cache
- func (cache *Cache) Close()
- func (cache *Cache) Count() int
- func (cache *Cache) Delete(key any)
- func (cache *Cache) Get(key string) (any, bool)
- func (cache *Cache) Load(key any) (any, bool)
- func (cache *Cache) Purge()
- func (cache *Cache) Range(cb func(k, v any) bool)
- func (cache *Cache) Remove(key string) bool
- func (cache *Cache) Set(key string, data any)
- func (cache *Cache) SetCheckExpirationCallback(callback checkExpireCallback)
- func (cache *Cache) SetExpirationCallback(callback expireCallback)
- func (cache *Cache) SetNewItemCallback(callback expireCallback)
- func (cache *Cache) SetTTL(ttl time.Duration)
- func (cache *Cache) SetWithTTL(key string, data any, ttl time.Duration)
- func (cache *Cache) SkipTtlExtensionOnHit(value bool)
- func (cache *Cache) Store(key any, value any)
- func (cache *Cache) StoreWithTTL(key any, value any, 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 struct {
// contains filtered or unexported fields
}
Cache is a synchronized map of items that can auto-expire once stale
func NewTtlCache ¶
func NewTtlCache() *Cache
NewCache is a helper to create instance of the Cache struct
func (*Cache) Close ¶
func (cache *Cache) 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) Get ¶
Get is a thread-safe way to lookup items Every lookup, also touches the cachedItem, hence extending it's life
func (*Cache) SetCheckExpirationCallback ¶
func (cache *Cache) SetCheckExpirationCallback(callback checkExpireCallback)
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) SetExpirationCallback ¶
func (cache *Cache) SetExpirationCallback(callback expireCallback)
SetExpirationCallback sets a callback that will be called when an cachedItem expires
func (*Cache) SetNewItemCallback ¶
func (cache *Cache) SetNewItemCallback(callback expireCallback)
SetNewItemCallback sets a callback that will be called when a new cachedItem is added to the cache
func (*Cache) SetWithTTL ¶
SetWithTTL is a thread-safe way to add new items to the map with individual ttl
func (*Cache) 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.