Documentation
¶
Overview ¶
Package evcache provides a sync.Map wrapper with a number of dynamic features.
Index ¶
- Variables
- type Builder
- type Cache
- func (c *Cache) Close() error
- func (c *Cache) Evict(key interface{}) (value interface{}, evicted bool)
- func (c *Cache) Fetch(key interface{}, ttl time.Duration, f FetchCallback) (value interface{}, closer io.Closer, err error)
- func (c *Cache) Flush()
- func (c *Cache) Get(key interface{}) (value interface{}, closer io.Closer, exists bool)
- func (c *Cache) Len() int
- func (c *Cache) Set(key, value interface{}, ttl time.Duration) (replaced bool)
- type EvictionCallback
- type FetchCallback
Constants ¶
This section is empty.
Variables ¶
var EvictionInterval = time.Second
EvictionInterval is the interval for background loop.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder func(*Cache)
Builder builds a cache.
func New ¶
func New() Builder
New creates an empty cache.
Cache must be closed after usage has stopped to prevent a leaking goroutine.
It is not safe to close the cache while in use.
func (Builder) WithCapacity ¶
WithCapacity configures the cache with specified size limit. If cache exceeds the limit, the least frequently used records are evicted.
New records are inserted as the most frequently used to reduce premature eviction of new but unused records.
The maximum overflow at any given moment is the number of concurrent users. To limit overflow, limit concurrency.
func (Builder) WithEvictionCallback ¶
func (build Builder) WithEvictionCallback(cb EvictionCallback) Builder
WithEvictionCallback specifies an asynchronous eviction callback.
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a sync.Map wrapper with eventually consistent LFU eviction.
All methods except Close are safe to use concurrently.
func (*Cache) Close ¶
Close shuts down the cache, evicts all keys and waits for eviction callbacks to finish.
It is not safe to close the cache while in use.
func (*Cache) Fetch ¶
func (c *Cache) Fetch(key interface{}, ttl time.Duration, f FetchCallback) (value interface{}, closer io.Closer, err error)
Fetch attempts to get or set the value and calls f on a miss to receive the new value. If f returns an error, no value is cached and the error is returned back to caller.
When the returned value is not used anymore, the caller MUST call closer.Close() or a memory leak will occur.
If the cache has a size limit and it is exceeded, it may block until the overflow is evicted.
type EvictionCallback ¶
type EvictionCallback func(key, value interface{})
EvictionCallback is an asynchronous callback called after cache key eviction.
It is not called until all io.Closers returned alongside the value have been closed.
type FetchCallback ¶
type FetchCallback func() (interface{}, error)
FetchCallback is called when *Cache.Fetch has a miss.
It blocks the same key from being accessed until the callbak returns and must return a new value or an error.