evcache

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2021 License: MIT Imports: 5 Imported by: 0

README

evcache Go Reference

This README is a work in progress.

How it works

The cache is a wrapper for sync.Map with autoexpiry, capacity limit and eviction order. It acts like a non-blocking ordered map where writes do not block reads. Each value is wrapped in a record type and uses its own RWMutex.

The default record order is insertion order. Records are always inserted to the back of a list and when overflow occurs, the front element is evicted.

When LFU ordering is used, then records which had accumulated hits since its last move will be periodically moved forwards in the list based on the hit count delta. Eventually the list becomes LFU ordered.

Documentation

Overview

Package evcache provides an eventually consistent ordered cache.

Index

Constants

This section is empty.

Variables

View Source
var SyncInterval = time.Second

SyncInterval is the interval for background loop for autoexpiry and optional eventual LFU eviction ordering.

If cache overflows while LFU is enabled and records are created faster than SyncInterval can update record ordering, the eviction starts losing LFU order and will become the insertion order with eldest first.

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(capacity uint32) Builder

WithCapacity configures the cache with specified capacity.

If cache exceeds the limit, the eldest record is evicted.

func (Builder) WithEvictionCallback

func (build Builder) WithEvictionCallback(cb EvictionCallback) Builder

WithEvictionCallback specifies an asynchronous eviction callback.

func (Builder) WithLFU added in v1.4.0

func (build Builder) WithLFU() Builder

WithLFU enables the near-LFU eviction order of records.

New records are inserted as the most frequently used to reduce premature eviction of new but unused records.

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is an eventually consistent ordered cache.

By default, records are sorted by insertion order.

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{})

Evict a key.

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.

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) OrderedRange added in v1.4.0

func (c *Cache) OrderedRange(f func(key, value interface{}) bool)

OrderedRange calls f sequentially for each key and value present in the cache in order. If f returns false, Range stops the iteration. When LFU is used, the order is from least to most frequently used, otherwise it is the insertion order with eldest first by default.

It is not safe to use OrderedRange concurrently with any other method.

func (*Cache) Range added in v0.1.3

func (c *Cache) Range(f func(key, value interface{}) bool)

Range calls f for each key and value present in the cache in no particular order. If f returns false, Range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the cache's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.

func (*Cache) Set

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

Set a value in the cache for a key.

type EvictionCallback

type EvictionCallback func(key, value interface{})

EvictionCallback is an asynchronous callback which is called after cache key eviction.

It waits until all io.Closers returned by Get or Fetch are closed.

type FetchCallback

type FetchCallback func() (interface{}, error)

FetchCallback is called when *Cache.Fetch has a miss and must return a new value or an error.

It blocks the key from being accessed until the callback returns.

type List added in v1.4.0

type List struct {
	// contains filtered or unexported fields
}

List is a size-limited list using rings.

func NewList added in v1.4.0

func NewList(capacity uint32) *List

NewList creates an empty list.

func (*List) Do added in v1.4.0

func (l *List) Do(f func(value interface{}) bool)

Do calls function f on each element of the list, in forward order. If f returns false, Do stops the iteration. f must not change l.

func (*List) Len added in v1.4.0

func (l *List) Len() int

Len returns the length of elements in the ring.

func (*List) MoveForward added in v1.4.0

func (l *List) MoveForward(r *ring.Ring, delta uint32)

MoveForward moves element forward by at most delta positions.

func (*List) PushBack added in v1.4.0

func (l *List) PushBack(value interface{}, r *ring.Ring) (front interface{})

PushBack inserts a value at the back of list. If capacity is exceeded, an element from the front of list is removed and its value returned.

func (*List) Remove added in v1.4.0

func (l *List) Remove(r *ring.Ring) (value interface{})

Remove an element from the list.

Jump to

Keyboard shortcuts

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