evcache

package module
v1.0.1 Latest Latest
Warning

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

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

README

evcache Go Reference

This library is a work in progress.

How it works

The cache is a wrapper for sync.Map with autoexpiry, capacity limit and near-LFU eviction.

Writes to the cache do not block other reads. Each key uses its own RWMutex.

Documentation

Overview

Package evcache provides a non-blocking eventually consistent LFU cache.

Index

Constants

This section is empty.

Variables

View Source
var SyncInterval = time.Second

SyncInterval is the interval for background loop.

If cache overflows and records are created faster than SyncInterval, the LFU eviction starts losing order.

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 least frequently used record is evicted.

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

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 an eventually consistent LFU cache.

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) Range added in v0.1.3

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

Range calls f sequentially for each key and value present in the cache. 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.

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.

Jump to

Keyboard shortcuts

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