evcache

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2021 License: MIT Imports: 6 Imported by: 0

README

evcache Go Reference

How it works

The cache is a wrapper for sync.Map which means that all its concurrency guarantees apply. It additionally supports autoexpiry and capacity limit.

If the capacity is exceeded, the cache will begin evicting least frequently used records. It takes at least 1 turnover of the full capacity to reach 100% sortedness of the evicted keys.

The cache does not prevent capacity from being exceeded - the maximum overflow is the number of concurrent writers at any given moment.

To limit maximum overflow one must limit concurrency externally (for example with a channel semaphore).

Documentation

Overview

Package evcache provides a sync.Map wrapper with a number of dynamic features.

Index

Constants

This section is empty.

Variables

View Source
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) Build

func (build Builder) Build() *Cache

Build the cache.

func (Builder) WithCapacity

func (build Builder) WithCapacity(size uint32) Builder

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

func (c *Cache) Close() error

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) Evict

func (c *Cache) Evict(key interface{}) (value interface{}, evicted bool)

Evict a key and return its value if it exists.

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.

func (*Cache) Flush

func (c *Cache) Flush()

Flush evicts all keys from the cache.

func (*Cache) Get

func (c *Cache) Get(key interface{}) (value interface{}, closer io.Closer, exists bool)

Get returns the value stored in the cache for a key. The boolean indicates whether a value was found.

When the returned value is not used anymore, the caller MUST call closer.Close() or a memory leak will occur.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of keys in the cache.

func (*Cache) Set

func (c *Cache) Set(key, value interface{}, ttl time.Duration) (replaced bool)

Set the value for a key.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL