evcache

package module
v4.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 3 Imported by: 0

README

evcache

Go Reference Go Report Card

Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication.

Example
package main

import (
	"time"

	"github.com/mgnsk/evcache/v4"
)

func main() {
	c := evcache.New[string, string](
		evcache.WithCapacity(128),
		evcache.WithPolicy(evcache.LRU),
		evcache.WithTTL(time.Minute),
	)

	// Fetches an existing value or calls the callback to get a new value.
	result, err := c.Fetch("key", func() (string, error) {
		// Possibly a very long network call. It only blocks write access to this key.
		// Read access for this key returns as if the value does not exist.
		return "value", nil
	})

	if err != nil {
		panic(err)
	}

	// Use the result.
	println(result)
}

Documentation

Overview

Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication.

Index

Constants

View Source
const (
	// FIFO policy orders records in FIFO order.
	FIFO = backend.FIFO
	// LFU policy orders records in LFU order.
	LFU = backend.LFU
	// LRU policy orders records in LRU order.
	LRU = backend.LRU
)

Available cache eviction policies.

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 is an in-memory cache.

func New

func New[K comparable, V any](opt ...Option) *Cache[K, V]

New creates a new empty cache.

func (*Cache[K, V]) Evict

func (c *Cache[K, V]) Evict(key K) (value V, evicted bool)

Evict an initialized value.

func (*Cache[K, V]) Fetch

func (c *Cache[K, V]) Fetch(key K, f func() (V, error)) (value V, err error)

Fetch loads or stores (and initializes) a value for key with the default TTL. If a value exists, f will not be called, otherwise f will be called to fetch the new value. It panics if f panics. Concurrent fetches for the same key will block and return a single result.

func (*Cache[K, V]) FetchTTL

func (c *Cache[K, V]) FetchTTL(key K, f func() (V, time.Duration, error)) (value V, err error)

FetchTTL loads or stores (and initializes) a value for key with the specified TTL. If a value exists, f will not be called, otherwise f will be called to fetch the new value. It panics if f panics. Concurrent fetches for the same key will block and return a single result.

func (*Cache[K, V]) Has

func (c *Cache[K, V]) Has(key K) bool

Has returns whether the value for key is initialized.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len returns the number of initialized values.

func (*Cache[K, V]) Load

func (c *Cache[K, V]) Load(key K) (value V, loaded bool)

Load an initialized value.

func (*Cache[K, V]) Range

func (c *Cache[K, V]) Range(f func(key K, value V) bool)

Range iterates over initialized cache key-value pairs in no particular order or consistency. If f returns false, range stops the iteration.

f may modify the cache.

func (*Cache[K, V]) Store

func (c *Cache[K, V]) Store(key K, value V)

Store and initialize a value with the default TTL.

func (*Cache[K, V]) StoreTTL

func (c *Cache[K, V]) StoreTTL(key K, value V, ttl time.Duration)

StoreTTL stores and initializes a value with specified TTL.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is a cache configuration option.

func WithCapacity

func WithCapacity(capacity int) Option

WithCapacity option configures the cache with specified capacity.

The zero values configures unbounded capacity.

func WithPolicy

func WithPolicy(policy string) Option

WithPolicy option configures the cache with specified eviction policy.

The zero value configures the FIFO policy.

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL option configures the cache with specified default TTL.

The zero value configures infinite default TTL.

Directories

Path Synopsis
internal
backend
Package backend implements cache backend.
Package backend implements cache backend.
Package list implements a doubly linked list with zero list initialization.
Package list implements a doubly linked list with zero list initialization.

Jump to

Keyboard shortcuts

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