Documentation
¶
Overview ¶
Package cache implements an expiring in-memory cache.
Deprecated: moved to codeberg.org/clambin/go-common
Index ¶
- type Cache
- func (c *Cache[K, V]) Add(key K, value V)
- func (c *Cache[K, V]) AddWithExpiry(key K, value V, expiry time.Duration)
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetAndRemove(key K) (V, bool)
- func (c *Cache[K, V]) GetDefaultExpiration() time.Duration
- func (c *Cache[K, V]) Iterate() iter.Seq2[K, V]
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Remove(key K)
- func (c *Cache[K, V]) Size() int
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 }
Cache implements a generic cache with expiration timers for entries, with optional removal of expired items.
func New ¶
func New[K comparable, V any](expiration, cleanup time.Duration) *Cache[K, V]
New creates a new Cache for the specified key and value types. The expiration parameter specifies the default time an entry can live in the cache before expiring. The cleanup parameters specifies how often the cache will remove expired items.
func (*Cache[K, V]) Add ¶
func (c *Cache[K, V]) Add(key K, value V)
Add adds a key/value pair to the cache, using the default expiry time
func (*Cache[K, V]) AddWithExpiry ¶
AddWithExpiry adds a key/value pair to the cache with a specified expiration timer
func (*Cache[K, V]) Get ¶
Get returns the value from the cache for the provided key. If the item is not found, or expired, found will be false
func (*Cache[K, V]) GetAndRemove ¶ added in v0.5.0
GetAndRemove returns the value from the cache and removes it in one atomic operation.
func (*Cache[K, V]) GetDefaultExpiration ¶
GetDefaultExpiration returns the default expiration time of the cache
func (*Cache[K, V]) Iterate ¶ added in v0.7.0
Iterate returns an iterator that yields all non-expired keys & they value.
Note: the cache is locked while the iterator is running.