Documentation
¶
Overview ¶
Cache works as thread-safe least recently used (LRU) cache with expiring items on a given TTL span. The TTL gets automatically cycled on each Get() call, which permits the item to stay longer in the cache. There is a soft cap by default on the cache items that get rotated out based on the expiry date.
Index ¶
- Constants
- func Debug() bool
- type Cache
- func (c *Cache[Key, Value]) Count() uint
- func (c *Cache[Key, Value]) Delete(key Key) bool
- func (c *Cache[Key, Value]) EnableHardCap()
- func (c *Cache[Key, Value]) Expire(key Key, data Value) error
- func (c *Cache[Key, Value]) Get(key Key) (Value, bool)
- func (c *Cache[Key, Value]) Info() []Info[Key]
- func (c *Cache[Key, Value]) Max() uint
- func (c *Cache[Key, Value]) Once(key Key, data Value) error
- func (c *Cache[Key, Value]) Persist(key Key, data Value) error
- func (c *Cache[Key, Value]) Set(key Key, data Value) error
- func (c *Cache[Key, Value]) Wipe() bool
- type Info
Constants ¶
const ( // Null disables the ttl Null time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache[Key comparable, Value any] struct { // Logger defaults to stderr and adds a prefix of "cache: " Logger *log.Logger // contains filtered or unexported fields }
Cache holds all items and configuration.
func New ¶
New creates a new cache instance, taking a ttl and max number for the containing items.
ttl = Null | never expires [~100 years] max = 0 | fallback to default maxItems [64]
func (*Cache[Key, Value]) Delete ¶
Delete removes the item called by the name of key from the cache.
func (*Cache[Key, Value]) EnableHardCap ¶
func (c *Cache[Key, Value]) EnableHardCap()
EnableHardCap disables the ability to rotate the oldest item out of cache to be replaced by a new item.
func (*Cache[Key, Value]) Expire ¶
Expire adds a new item to the cache that expires after the TTL is reached, independent of Get calls.
func (*Cache[Key, Value]) Once ¶
Once adds new item to the cache that expires after the first cycle and gets no update on a Get call.