Documentation
¶
Index ¶
- type Cache
- func (c *Cache[K, V]) Close()
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetExpires(key K) (V, time.Time, bool)
- func (c *Cache[K, V]) Range(f func(key K, value V) bool)
- func (c *Cache[K, V]) Set(key K, value V, expires time.Duration)
- func (c *Cache[K, V]) SetAbs(key K, value V, expires time.Time)
- func (c *Cache[K, V]) SetPermanent(key K, value V)
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 manages items in a sync.Map, items are allowed to be permanent or have an expiry time. The keys which have an expiry time are stored in chain. chainAdd and chainDel are used to add/remove items to/from chain. These actions are completed in the cleaner goroutine to be concurrency safe.
func New ¶
func New[K comparable, V any]() *Cache[K, V]
New creates a *Cache[K, V] ready to be used by the caller
func (*Cache[K, V]) Close ¶
func (c *Cache[K, V]) Close()
Close simply sends a signal to stop the cleaner goroutine. The cache is left in its current state and can still be read. The only difference being that items which have expired will not be garbage collected.
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete removes an item from the cache.
func (*Cache[K, V]) Get ¶
Get returns (value, true) for any existing key. Or (V{}, false) for any unknown key. This is equivalent to calling GetExpires and ignoring the expiry time return value.
func (*Cache[K, V]) GetExpires ¶
GetExpires returns (value, expiry, true) for any existing key. Or (V{}, time.Time{}, false) for any unknown key.
func (*Cache[K, V]) Range ¶
Range calls f with every key-value pair, which has not expired, currently stored in the map.
If f returns false, range stops the iteration.
See sync/Map.Range for implementation specific details.
func (*Cache[K, V]) Set ¶
Set adds an item to the cache with an expiry duration.
If an item is added with the same key then this item with be overwritten.
func (*Cache[K, V]) SetAbs ¶ added in v0.0.8
SetAbs adds an item to the cache with an expiry date.
If an item is added with the same key then this item with be overwritten.
func (*Cache[K, V]) SetPermanent ¶
func (c *Cache[K, V]) SetPermanent(key K, value V)
SetPermanent adds an item to the cache without an expiry date.
If an item is added with the same key then this item with be overwritten.