Documentation
¶
Index ¶
- type Cache
- func (c Cache) Add(k K, v V) bool
- func (c Cache) AddWithTTL(k K, v V, d time.Duration) bool
- func (c Cache) Clean()
- func (c Cache) Clear()
- func (c Cache) Each(f func(K, Item[V]) bool)
- func (c Cache) Get(k K) (V, bool)
- func (c Cache) GetWithTTL(k K) (V, time.Time, bool)
- func (c Cache) Increment(k K, n V) V
- func (c Cache) Items() map[K]Item[V]
- func (c Cache) Len() int
- func (c Cache) Remove(k K)
- func (c Cache) Replace(k K, v V) bool
- func (c Cache) ReplaceWithTTL(k K, v V, d time.Duration) bool
- func (c Cache) Set(k K, v V)
- func (c Cache) SetWithTTL(k K, v V, d time.Duration)
- type Item
- type Worker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
func New ¶
func New[K comparable, V any](defaultExpires, cleanupInterval time.Duration) *Cache[K, V]
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.Clean().
func NewFrom ¶
func NewFrom[K comparable, V any](defaultExpires, cleanupInterval time.Duration, items map[K]Item[V]) *Cache[K, V]
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.Clean().
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 ¶
func (c Cache) Add(k K, v V) bool
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 false otherwise.
func (Cache) AddWithTTL ¶ added in v1.0.27
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 false otherwise.
func (Cache) Clean ¶ added in v1.0.27
func (c Cache) Clean()
Clean Remove all expired items from the cache.
func (Cache) Each ¶ added in v1.0.27
Each Iterate all unexpired items in the cache to call function `f`. Function `f` returns false can break th iteration.
func (Cache) Get ¶
func (c Cache) Get(k K) (V, bool)
Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.
func (Cache) GetWithTTL ¶ added in v1.0.27
GetWithTTL 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
func (c Cache) Increment(k K, n V) V
Increment an item of type int, int8, int16, int32, int64, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns the incremented value.
func (Cache) Items ¶
func (c Cache) Items() map[K]Item[V]
Items Copies all unexpired items in the cache into a new map and returns it.
func (Cache) Len ¶ added in v1.0.27
func (c Cache) Len() int
Len Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.
func (Cache) Remove ¶ added in v1.0.27
func (c Cache) Remove(k K)
Remove an item from the cache. Does nothing if the key is not in the cache.
func (Cache) Replace ¶
func (c Cache) Replace(k K, v V) bool
Replace a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns false otherwise.
func (Cache) ReplaceWithTTL ¶ added in v1.0.27
Replace a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns false otherwise.
func (Cache) Set ¶
func (c Cache) Set(k K, v V)
Set an item to the cache, replacing any existing item. The cache's default expiration time is used.
func (Cache) SetWithTTL ¶ added in v1.0.27
Set an item to the cache, replacing any existing item. If the duration is 0, the cache's default expiration time is used. If it < 0, the item never expires.