Documentation ¶
Index ¶
- Constants
- type Cache
- func (c *Cache[V]) Close()
- func (c *Cache[V]) Delete(k string)
- func (c *Cache[V]) DeleteExpired()
- func (c *Cache[V]) Flush()
- func (c *Cache[V]) Get(k string) (val V, ok bool)
- func (c *Cache[V]) ItemCount() int
- func (c *Cache[V]) Iterate(it func(key string, v V))
- func (c *Cache[V]) OnEvicted(f func(string, V))
- func (c *Cache[V]) Set(k string, x V)
- func (c *Cache[V]) SetWithExpiration(k string, x V, d time.Duration)
- type Item
Constants ¶
const ( // For use with functions that take an expiration time. NoExpiration time.Duration = -1 // For use with functions that take an expiration time. Equivalent to // passing in the same expiration duration as was given to New() or // NewFrom() when the cache was created (e.g. 5 minutes.) DefaultExpiration time.Duration = 0 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[V any] struct { // contains filtered or unexported fields }
func New ¶
Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().
func (*Cache[V]) Delete ¶
Delete an item from the cache. Does nothing if the key is not in the cache.
func (*Cache[V]) DeleteExpired ¶
func (c *Cache[V]) DeleteExpired()
Delete all expired items from the cache.
func (*Cache[V]) Get ¶
Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.
func (*Cache[V]) ItemCount ¶
Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.
func (*Cache[V]) OnEvicted ¶
Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.