Documentation ¶
Index ¶
- Constants
- type Cache
- func (c Cache) Add(k string, x V, d time.Duration) error
- func (c Cache) Delete(k string)
- func (c Cache) DeleteExpired()
- func (c Cache) Flush()
- func (c Cache) Get(k string) (V, bool)
- func (c Cache) GetWithExpiration(k string) (V, time.Time, bool)
- func (c Cache) ItemCount() int
- func (c Cache) Items() map[string]Item[V]
- func (c Cache) OnEvicted(f func(string, V))
- func (c Cache) Replace(k string, x V, d time.Duration) error
- func (c Cache) Set(k string, x V, d time.Duration)
- func (c Cache) SetDefault(k string, x V)
- type Item
- type Number
- type NumberCache
- func (c NumberCache) Add(k string, x V, d time.Duration) error
- func (c *NumberCache[T]) Decrement(k string, n T) (T, error)
- func (c NumberCache) Delete(k string)
- func (c NumberCache) DeleteExpired()
- func (c NumberCache) Flush()
- func (c NumberCache) Get(k string) (V, bool)
- func (c NumberCache) GetWithExpiration(k string) (V, time.Time, bool)
- func (c *NumberCache[T]) Increment(k string, n T) (T, error)
- func (c NumberCache) ItemCount() int
- func (c NumberCache) Items() map[string]Item[V]
- func (c NumberCache) OnEvicted(f func(string, V))
- func (c NumberCache) Replace(k string, x V, d time.Duration) error
- func (c NumberCache) Set(k string, x V, d time.Duration)
- func (c NumberCache) SetDefault(k string, x V)
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 NewFrom ¶
func NewFrom[V any](defaultExpiration, cleanupInterval time.Duration, items map[string]Item[V]) *Cache[V]
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().
NewFrom() also accepts an items map which will serve as the underlying map for the cache. This is useful for starting from a deserialized cache (serialized using e.g. gob.Encode() on c.Items()), or passing in e.g. make(map[string]Item, 500) to improve startup performance when the cache is expected to reach a certain minimum size.
Only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.Items() (subject to the same caveat.)
Note regarding serialization: When using e.g. gob, make sure to gob.Register() the individual types stored in the cache before encoding a map retrieved with c.Items(), and to register those same types before decoding a blob containing an items map.
func (Cache) Add ¶
Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.
func (Cache) Delete ¶
func (c Cache) Delete(k string)
Delete an item from the cache. Does nothing if the key is not in the cache.
func (Cache) DeleteExpired ¶
func (c Cache) DeleteExpired()
Delete all expired items from the cache.
func (Cache) Get ¶
Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.
func (Cache) GetWithExpiration ¶
GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.
func (Cache) ItemCount ¶
func (c Cache) ItemCount() int
Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.
func (Cache) OnEvicted ¶
func (c Cache) OnEvicted(f func(string, V))
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.
func (Cache) Replace ¶
Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.
func (Cache) Set ¶
Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
func (Cache) SetDefault ¶
func (c Cache) SetDefault(k string, x V)
Add an item to the cache, replacing any existing item, using the default expiration.
type NumberCache ¶
type NumberCache[T Number] struct { // contains filtered or unexported fields }
func NewNumberCache ¶
func NewNumberCache[T Number](defaultExpiration, cleanupInterval time.Duration) *NumberCache[T]
func (NumberCache) Add ¶
Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.
func (*NumberCache[T]) Decrement ¶
func (c *NumberCache[T]) Decrement(k string, n T) (T, error)
func (NumberCache) Delete ¶
func (c NumberCache) Delete(k string)
Delete an item from the cache. Does nothing if the key is not in the cache.
func (NumberCache) DeleteExpired ¶
func (c NumberCache) DeleteExpired()
Delete all expired items from the cache.
func (NumberCache) Get ¶
Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.
func (NumberCache) GetWithExpiration ¶
GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.
func (*NumberCache[T]) Increment ¶
func (c *NumberCache[T]) Increment(k string, n T) (T, error)
func (NumberCache) ItemCount ¶
func (c NumberCache) ItemCount() int
Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.
func (NumberCache) OnEvicted ¶
func (c NumberCache) OnEvicted(f func(string, V))
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.
func (NumberCache) Replace ¶
Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.
func (NumberCache) Set ¶
Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.
func (NumberCache) SetDefault ¶
func (c NumberCache) SetDefault(k string, x V)
Add an item to the cache, replacing any existing item, using the default expiration.
Directories ¶
Path | Synopsis |
---|---|
backedcache wraps github.com/barrett370/go-cache#Cache[V] with a backend, requests for keys not in the underlying cache will fetch (via a singleflight group to prevent stampeding) via a user-defined GetterFn[K,V]
|
backedcache wraps github.com/barrett370/go-cache#Cache[V] with a backend, requests for keys not in the underlying cache will fetch (via a singleflight group to prevent stampeding) via a user-defined GetterFn[K,V] |