Documentation
¶
Index ¶
- type Cache
- func (c Cache) Add(k string, x any, d ...time.Duration) error
- func (c Cache) Clear()
- func (c Cache) Count() int
- func (c Cache) Decrement(k string, n any, x ...any) error
- func (c Cache) Delete(k string)
- func (c Cache) DeleteExpired()
- func (c Cache) Get(k string) (any, bool)
- func (c Cache) GetWithExpires(k string) (any, time.Time, bool)
- func (c Cache) Increment(k string, n any, x ...any) error
- func (c Cache) Items() map[string]Item
- func (c Cache) Replace(k string, x any, d ...time.Duration) error
- func (c Cache) Set(k string, x any, d ...time.Duration)
- type Item
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache 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 1, the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than 1, expired items are not deleted from the cache before calling c.DeleteExpired().
func NewFrom ¶
Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than 1, the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than 1, 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 preallocated cache like 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.)
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) Count ¶
func (c Cache) Count() 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) Decrement ¶ added in v1.0.15
Decrement an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found and default value 'x[0]' is not supplied, or if it is not possible to decrement it by n.
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) GetWithExpires ¶ added in v1.0.15
GetWithExpires 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) Increment ¶ added in v1.0.15
Increment an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found and default value 'x[0]' is not supplied, or if it is not possible to increment it by n.