cache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

README

go-cache

Two-level cache (local L1 + remote L2) with circuit breaker, Prometheus/OpenTelemetry metrics, and structured value support.

The remote layer is defined by the RemoteCache interface, so you can plug in Redis, Memcached, DynamoDB, or any other backend. A ready-to-use Redis adapter is included.

Install

go get github.com/treussart/go-cache

Architecture

Get request
    │
    ▼
┌────────┐  hit   ┌────────────┐
│ L1     │───────▶│ return     │
│ (local)│        └────────────┘
└───┬────┘
    │ miss
    ▼
┌─────────┐  hit   ┌────────────┐
│ L2      │───────▶│ write to L1│──▶ return
│ (remote)│        └────────────┘
└───┬─────┘
    │ miss
    ▼
ErrCacheMiss

Either layer is optional — you can use L1-only, L2-only, or both.

Usage

Cache instances are created with functional options:

import (
    cache "github.com/treussart/go-cache"
)

c, err := cache.New("my-service",
    cache.WithRedisConn(redisClient, 5*time.Minute),
    cache.WithLocalCacheTinyLFU(10000, time.Minute),
    cache.WithCBEnabled(true),
)
Using a custom remote cache

Implement the RemoteCache interface for any backend and pass it with WithRemoteCache:

c, err := cache.New("my-service",
    cache.WithRemoteCache(myMemcachedAdapter, 5*time.Minute),
    cache.WithLocalCacheTinyLFU(10000, time.Minute),
)
Raw bytes
err = c.Set(ctx, []byte("user:1"), []byte("data"))

val, err := c.Get(ctx, []byte("user:1"))

err = c.SetExp(ctx, []byte("user:1"), []byte("data"), 30*time.Second)

err = c.Del(ctx, []byte("user:1"))
Structured values

GetStruct / SetStruct / SetExStruct encode and decode values automatically using the configured Coder (MsgPack by default):

type User struct {
    Name string
    Age  int
}

err = c.SetStruct(ctx, "user:1", &User{Name: "Alice", Age: 30})

var u User
err = c.GetStruct(ctx, "user:1", &u)

err = c.SetExStruct(ctx, "user:1", &User{Name: "Alice"}, 30*time.Second)

Functional options

Option Description
WithRemoteCache(rc, ttl) Set the remote cache implementation (RemoteCache) and default TTL
WithRedisConn(conn, ttl) Convenience: set the remote cache to a Redis connection (redis.UniversalClient) and default TTL
WithLocalCacheTinyLFU(size, ttl) L1 using TinyLFU eviction (size = max items, default 10 000)
WithLocalCacheFreeCache(size, ttl) L1 using FreeCache (size = bytes, default 1 MB)
WithPrefixKey(prefix) Override key namespace prefix (default: name + ":")
WithCoder(coder) Serializer for *Struct methods (default: MsgPackCoder)
WithStatsProm(stats) Attach Prometheus counters from GetStatsProm()
WithStatsOTEL(stats) Attach OpenTelemetry counters from GetStatsOTEL()
WithCBEnabled(bool) Enable circuit breaker on remote cache calls
WithCBTimeout(d) Time in open state before half-open probe (default: 4 min)
WithCBMaxRequests(n) Max requests allowed in half-open state (default: 1)
WithCBConsecutiveFailures(n) Consecutive failures before tripping (default: 2)
WithGracefulDegradation(staleTTL, staleCacheSize...) Enable stale cache fallback; 0 TTL = never expire, optional size (default 10k)
WithPreload(data) Warm up L1 on startup with initial key-value pairs (see below)

Interfaces

Cacher
type Cacher interface {
    Get(ctx context.Context, key []byte) ([]byte, error)
    Set(ctx context.Context, key, value []byte) error
    SetExp(ctx context.Context, key, value []byte, ttl time.Duration) error
    Del(ctx context.Context, key []byte) error
    DeleteFromLocalCache(key []byte)
    DeleteFromRemoteCache(ctx context.Context, key []byte) error
    Ready(ctx context.Context) error
    GetStruct(ctx context.Context, key string, dest any) error
    SetStruct(ctx context.Context, key string, value any) error
    SetExStruct(ctx context.Context, key string, value any, ttl time.Duration) error
}

A Mocked struct (testify mock) implementing Cacher is provided for unit tests.

RemoteCache
type RemoteCache interface {
    Get(ctx context.Context, key string) ([]byte, error)
    Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
    Del(ctx context.Context, key string) error
    Ping(ctx context.Context) error
}

Get must return ErrCacheMiss when the key does not exist.

A RedisRemoteCache adapter implementing RemoteCache is included. Create one with NewRedisRemoteCache(client) or use the WithRedisConn convenience option.

LocalCache
type LocalCache interface {
    Set(key []byte, data []byte) error
    SetExp(key []byte, data []byte, ttl time.Duration) error
    Get(key []byte) ([]byte, error)
    Del(key []byte)
}

Built-in implementations: TinyLFU and FreeCache.

Circuit breaker

When enabled, all remote cache operations go through a gobreaker circuit breaker:

  • Closed — requests flow normally to the remote cache.
  • Open — remote calls are skipped. Get returns the error; Set/SetExp/Del silently fall back to L1.
  • Half-open — a limited number of probe requests are sent to the remote cache.

ErrCacheMiss (cache miss) is treated as a success and does not count toward tripping.

Graceful degradation

When the remote cache goes down and the circuit breaker opens, Get normally returns an error for any key that is no longer in L1 (expired or evicted). With graceful degradation enabled, a stale cache (a separate in-memory TinyLFU with a longer TTL) is consulted before returning an error:

CB open + L1 miss
    │
    ▼
┌────────────┐  hit   ┌─────────────────┐
│ Stale cache│───────▶│ return stale data│
└───┬────────┘        └─────────────────┘
    │ miss
    ▼
return error

Enable it with:

c, err := cache.New("my-service",
    cache.WithRedisConn(redisClient, 5*time.Minute),
    cache.WithLocalCacheTinyLFU(10000, time.Minute),
    cache.WithCBEnabled(true),
    cache.WithGracefulDegradation(1*time.Hour), // stale TTL
)
  • The stale cache is written on every Set/SetExp/SetStruct/SetExStruct, not only during degradation.
  • The stale cache is consulted on any remote cache error (connection failure, timeout, CB open/too-many-requests). Only ErrCacheMiss (genuine cache miss) is not covered.
  • Del clears both the primary L1 and the stale cache to maintain consistency.
  • A cache_stale_hit_total metric is emitted on each stale hit (both Prometheus and OpenTelemetry).
  • A staleTTL of 0 means entries never expire (they are only evicted when the cache is full).
  • Stale eviction — the stale TinyLFU holds 10k items by default (override with the optional second argument, e.g. WithGracefulDegradation(0, 50000)). If you write more distinct keys than the configured size, the LFU eviction policy may evict the preloaded fallback.

Cache preloading

Warm up L1 on startup so the first requests hit the local cache instead of going to the remote cache:

data := map[string][]byte{
    "config:feature-flags": flagsJSON,
    "config:rate-limits":   limitsJSON,
}

c, err := cache.New("my-service",
    cache.WithLocalCacheTinyLFU(10000, time.Minute),
    cache.WithRedisConn(redisClient, 5*time.Minute),
    cache.WithPreload(data),
)
  • Data is written to L1 (and the stale cache if WithGracefulDegradation is also enabled).
  • The remote cache is not touched — preloading is strictly for the local layer.
  • Keys are subject to the configured prefix, just like regular Set calls.

Maximum resiliency example

Combine circuit breaker, graceful degradation with a never-expiring stale cache, and preloading with a fallback value to guarantee that Get always returns data — even if the remote cache has never been reachable:

// A sensible default that is returned when both L1 and remote are unavailable
// and no real value has ever been written for this key.
fallback := map[string][]byte{
    "config": []byte(`{"feature_x":false}`),
}

c, err := cache.New("my-service",
    cache.WithRedisConn(redisClient, 5*time.Minute),
    cache.WithLocalCacheTinyLFU(10000, time.Minute),
    cache.WithCBEnabled(true),
    cache.WithGracefulDegradation(0),  // stale entries never expire
    cache.WithPreload(fallback),       // warm L1 + stale cache on startup
)

What happens at runtime:

  1. Normal operationGet("config") hits L1 or the remote cache as usual.
  2. L1 expires, remote healthy — value is fetched from the remote cache and written back to L1.
  3. Remote goes down, L1 still fresh — L1 hit, no error.
  4. Remote down, L1 expired — circuit breaker is open, stale cache returns the last known value.
  5. Remote has never been reachable — the preloaded fallback is still in the stale cache (TTL = 0, never expires) and is returned.

This makes the cache behave as a best-effort data source that degrades gracefully rather than failing.

Coders

The Coder interface controls serialization for GetStruct/SetStruct/SetExStruct:

Implementation Format
MsgPackCoder MessagePack (default)
JSONCoder JSON

Implement the Coder interface for custom serialization:

type Coder interface {
    Encode(value any) ([]byte, error)
    Decode(data []byte, value any) error
}

Observability

Prometheus
c, _ := cache.New("my-cache",
    cache.WithStatsProm(cache.GetStatsProm("namespace", "subsystem")),
    // ...
)
OpenTelemetry
stats, _ := cache.GetStatsOTEL("my-cache")
c, _ := cache.New("my-cache",
    cache.WithStatsOTEL(stats),
    // ...
)

Exposed metrics: cache_local_hit_total, cache_local_miss_total, cache_remote_hit_total, cache_remote_miss_total, cache_stale_hit_total, cache_local_set_total, cache_remote_set_total, cache_set_total, cache_cb_open_total, cache_cb_too_many_requests_total, cache_cb_state, cache_duration_seconds.

All metrics are labelled with cache_name.

Benchmarks

Run with go test -bench=. -benchmem ./...

Apple M1 Pro — Go 1.26 — arm64

Coder (serialization only)
Benchmark ns/op B/op allocs/op
MsgPackCoder Encode 503 608 5
MsgPackCoder Decode 712 312 9
JSONCoder Encode (goccy/go-json) 281 320 2
JSONCoder Decode (goccy/go-json) 453 416 6
Cache operations (L1 only, raw bytes)
Benchmark ns/op B/op allocs/op Δ ns Δ allocs
FreeCache Set 206 240 3 −26 % −40 %
FreeCache Get (hit) 217 256 4 −38 % −43 %
FreeCache Get (miss) 257 304 5 −36 % −38 %
TinyLFU Set 434 440 7 −22 % −22 %
TinyLFU Get (hit) 216 240 3 −38 % −50 %
TinyLFU Get (miss) 155 240 3 −46 % −50 %
### Cache operations with graceful degradation (L1 + stale cache, raw bytes)
Benchmark ns/op B/op allocs/op vs. without GD
FreeCache Set 521 440 7 +4 allocs (stale TinyLFU write)
FreeCache Get (hit) 260 256 4 0 allocs overhead
FreeCache Get (miss) 288 304 5 0 allocs overhead

Set pays for the extra TinyLFU write into the stale cache. Get paths are unaffected because the stale cache is only consulted when the circuit breaker rejects a request.

Cache operations (L1 only, struct roundtrip)
Benchmark ns/op B/op allocs/op Δ ns Δ allocs
SetStruct MsgPack 403 360 6 −42 % −50 %
GetStruct MsgPack 516 424 9 −41 % −44 %
SetStruct JSON 312 312 5 −48 % −55 %
GetStruct JSON 390 424 7 −45 % −50 %

Documentation

Overview

Package cache provides a two-level cache (local L1 + remote L2) with circuit breaker, Prometheus/OpenTelemetry metrics, and structured value support.

Index

Constants

View Source
const (
	// LabelName is the Prometheus/OTEL label key used to identify the cache instance.
	LabelName = "cache_name"
)

Variables

View Source
var (
	// ErrCacheMiss is returned when the requested key is not found in the cache.
	ErrCacheMiss = errors.New("cache key is missing")

	// ErrKeyEmpty is returned when an empty key is provided to a cache operation.
	ErrKeyEmpty = errors.New("key is empty")

	// ErrInitCache is returned when cache initialization fails due to missing RemoteCache and LocalCache.
	ErrInitCache = errors.New("can not init cache : RemoteCache or LocalCache must not be nil")

	// ErrConvertingToBytes is returned when a cache value cannot be converted to []byte.
	ErrConvertingToBytes = errors.New("type error when converting to []byte")
)

Functions

This section is empty.

Types

type Cache

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

Cache implements Cacher with optional local (L1) and remote (L2) layers.

func New

func New(name string, options ...CustomOption) (*Cache, error)

New creates a new Cache. At least one of RemoteCache or LocalCache must be set.

func (*Cache) Del

func (c *Cache) Del(ctx context.Context, key []byte) error

Del removes a key from both local and remote caches.

func (*Cache) DeleteFromLocalCache

func (c *Cache) DeleteFromLocalCache(key []byte)

DeleteFromLocalCache removes a key from the local cache and the stale cache.

func (*Cache) DeleteFromRemoteCache

func (c *Cache) DeleteFromRemoteCache(ctx context.Context, key []byte) error

DeleteFromRemoteCache removes a key from the remote cache only.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key []byte) ([]byte, error)

Get retrieves a value by key, checking the local cache first, then falling back to the remote cache.

func (*Cache) GetStruct

func (c *Cache) GetStruct(ctx context.Context, key string, dest any) error

GetStruct retrieves a value by key and decodes it into dest.

func (*Cache) Ready

func (c *Cache) Ready(ctx context.Context) error

Ready checks whether the remote cache connection is healthy.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key, value []byte) error

Set stores a key-value pair in both local and remote caches using the configured TTL.

func (*Cache) SetExStruct

func (c *Cache) SetExStruct(ctx context.Context, key string, value any, ttl time.Duration) error

SetExStruct encodes value and stores it in the cache with a custom TTL.

func (*Cache) SetExp

func (c *Cache) SetExp(ctx context.Context, key, value []byte, ttl time.Duration) error

SetExp stores a key-value pair in both local and remote caches with a custom TTL.

func (*Cache) SetStruct

func (c *Cache) SetStruct(ctx context.Context, key string, value any) error

SetStruct encodes value and stores it in the cache under the given key.

type Cacher

type Cacher interface {
	Del(ctx context.Context, key []byte) error
	Set(ctx context.Context, key, value []byte) error
	SetExp(ctx context.Context, key, value []byte, ttl time.Duration) error
	Get(ctx context.Context, key []byte) ([]byte, error)
	DeleteFromLocalCache(key []byte)
	DeleteFromRemoteCache(ctx context.Context, key []byte) error
	Ready(ctx context.Context) error
	GetStruct(context.Context, string, any) error
	SetStruct(context.Context, string, any) error
	SetExStruct(context.Context, string, any, time.Duration) error
}

Cacher defines the interface for a two-level cache (local + remote).

type Coder

type Coder interface {
	Encode(value any) ([]byte, error)
	Decode(data []byte, value any) error
}

Coder defines the interface for encoding and decoding cache values.

type CustomOption

type CustomOption func(*customConfig)

CustomOption is a functional option for configuring a simple Cache.

func WithCBConsecutiveFailures

func WithCBConsecutiveFailures(v uint32) CustomOption

WithCBConsecutiveFailures sets the number of consecutive failures before the circuit breaker trips.

func WithCBEnabled

func WithCBEnabled(v bool) CustomOption

WithCBEnabled enables or disables the circuit breaker for remote cache operations.

func WithCBMaxRequests

func WithCBMaxRequests(v uint32) CustomOption

WithCBMaxRequests sets the maximum number of requests allowed in the half-open state.

func WithCBTimeout

func WithCBTimeout(v time.Duration) CustomOption

WithCBTimeout sets the circuit breaker timeout duration.

func WithCoder

func WithCoder(v Coder) CustomOption

WithCoder sets the encoder/decoder used for cache value serialization.

func WithGracefulDegradation

func WithGracefulDegradation(staleTTL time.Duration, staleCacheSize ...int) CustomOption

WithGracefulDegradation enables a stale cache that is consulted when the circuit breaker is open and the primary L1 misses. staleTTL controls how long entries survive in the stale cache (should be significantly longer than the primary L1 TTL). A zero staleTTL means entries never expire (eviction still applies when the cache is full). staleCacheSize sets the maximum number of items in the stale TinyLFU cache; 0 defaults to 10 000.

func WithLocalCacheFreeCache

func WithLocalCacheFreeCache(cacheSize int, localCacheTTL time.Duration) CustomOption

WithLocalCacheFreeCache configures a FreeCache-based local cache with the given size and TTL.

func WithLocalCacheTinyLFU

func WithLocalCacheTinyLFU(cacheSize int, localCacheTTL time.Duration) CustomOption

WithLocalCacheTinyLFU configures a TinyLFU-based local cache with the given size and TTL.

func WithPrefixKey

func WithPrefixKey(v []byte) CustomOption

WithPrefixKey overrides the key namespace prefix. Pass nil to disable prefixing.

func WithPreload

func WithPreload(data map[string][]byte) CustomOption

WithPreload warms up the local cache (and stale cache if configured) on startup with the provided key-value pairs. Data is written to L1 only; the remote cache is not touched. Keys are subject to the configured prefix.

func WithRedisConn

func WithRedisConn(conn redis.UniversalClient, remoteCacheTTL time.Duration) CustomOption

WithRedisConn sets the remote cache to a Redis connection.

func WithRemoteCache added in v1.0.1

func WithRemoteCache(rc RemoteCache, remoteCacheTTL time.Duration) CustomOption

WithRemoteCache sets the remote cache (L2) implementation and its default TTL.

func WithStatsOTEL

func WithStatsOTEL(stats *StatsOTEL) CustomOption

WithStatsOTEL sets the OpenTelemetry stats collector.

func WithStatsProm

func WithStatsProm(stats *StatsProm) CustomOption

WithStatsProm sets the Prometheus stats collector.

type FreeCache

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

FreeCache is a LocalCache implementation backed by freecache.

func NewFreeCache

func NewFreeCache(size int, ttl time.Duration) *FreeCache

NewFreeCache size is in bytes

func (*FreeCache) Del

func (c *FreeCache) Del(key []byte)

Del removes a key from the local cache.

func (*FreeCache) Get

func (c *FreeCache) Get(key []byte) ([]byte, error)

Get retrieves a value by key from the local cache.

func (*FreeCache) Set

func (c *FreeCache) Set(key []byte, b []byte) error

Set stores a key-value pair in the local cache using the default TTL.

func (*FreeCache) SetExp

func (c *FreeCache) SetExp(key []byte, b []byte, ttl time.Duration) error

SetExp stores a key-value pair in the local cache with a custom TTL.

type JSONCoder

type JSONCoder struct{}

JSONCoder encodes and decodes cache values using JSON serialization.

func (*JSONCoder) Decode

func (*JSONCoder) Decode(data []byte, value any) error

Decode deserializes JSON data into value.

func (*JSONCoder) Encode

func (*JSONCoder) Encode(value any) ([]byte, error)

Encode serializes value into JSON bytes.

type LocalCache

type LocalCache interface {
	Set(key []byte, data []byte) error
	SetExp(key []byte, data []byte, ttl time.Duration) error
	Get(key []byte) ([]byte, error)
	Del(key []byte)
}

LocalCache defines the interface for a local in-process cache (L1 layer).

type Mocked

type Mocked struct {
	mock.Mock
}

Mocked is a testify mock implementing Cacher for unit tests.

func (*Mocked) Del

func (m *Mocked) Del(ctx context.Context, key []byte) error

Del implements Cacher.

func (*Mocked) DeleteFromLocalCache

func (m *Mocked) DeleteFromLocalCache(key []byte)

DeleteFromLocalCache implements Cacher.

func (*Mocked) DeleteFromRemoteCache

func (m *Mocked) DeleteFromRemoteCache(ctx context.Context, key []byte) error

DeleteFromRemoteCache implements Cacher.

func (*Mocked) Get

func (m *Mocked) Get(ctx context.Context, key []byte) ([]byte, error)

Get implements Cacher.

func (*Mocked) GetStruct

func (m *Mocked) GetStruct(ctx context.Context, key string, dest any) error

GetStruct implements Cacher.

func (*Mocked) Ready

func (m *Mocked) Ready(ctx context.Context) error

Ready implements Cacher.

func (*Mocked) Set

func (m *Mocked) Set(ctx context.Context, key, value []byte) error

Set implements Cacher.

func (*Mocked) SetExStruct

func (m *Mocked) SetExStruct(ctx context.Context, key string, value any, ttl time.Duration) error

SetExStruct implements Cacher.

func (*Mocked) SetExp

func (m *Mocked) SetExp(ctx context.Context, key, value []byte, ttl time.Duration) error

SetExp implements Cacher.

func (*Mocked) SetStruct

func (m *Mocked) SetStruct(ctx context.Context, key string, value any) error

SetStruct implements Cacher.

type MsgPackCoder

type MsgPackCoder struct{}

MsgPackCoder encodes and decodes cache values using MessagePack serialization.

func (*MsgPackCoder) Decode

func (*MsgPackCoder) Decode(data []byte, value any) error

Decode deserializes MessagePack data into value.

func (*MsgPackCoder) Encode

func (*MsgPackCoder) Encode(value any) ([]byte, error)

Encode serializes value into MessagePack bytes.

type RedisRemoteCache added in v1.0.1

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

RedisRemoteCache adapts redis.UniversalClient to the RemoteCache interface.

func NewRedisRemoteCache added in v1.0.1

func NewRedisRemoteCache(client redis.UniversalClient) *RedisRemoteCache

NewRedisRemoteCache creates a RemoteCache backed by Redis.

func (*RedisRemoteCache) Del added in v1.0.1

func (r *RedisRemoteCache) Del(ctx context.Context, key string) error

Del removes a key.

func (*RedisRemoteCache) Get added in v1.0.1

func (r *RedisRemoteCache) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value by key. Returns ErrCacheMiss when the key does not exist.

func (*RedisRemoteCache) Ping added in v1.0.1

func (r *RedisRemoteCache) Ping(ctx context.Context) error

Ping checks whether the Redis connection is healthy.

func (*RedisRemoteCache) Set added in v1.0.1

func (r *RedisRemoteCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores a key-value pair with the given TTL.

type RemoteCache added in v1.0.1

type RemoteCache interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	Del(ctx context.Context, key string) error
	Ping(ctx context.Context) error
}

RemoteCache defines the interface for a remote cache (L2 layer). Get must return ErrCacheMiss when the requested key does not exist.

type StatsOTEL

type StatsOTEL struct {
	HitsLocal         metric.Float64Counter
	MissesLocal       metric.Float64Counter
	HitsRemote        metric.Float64Counter
	MissesRemote      metric.Float64Counter
	HitsStale         metric.Float64Counter
	SetLocal          metric.Float64Counter
	SetRemote         metric.Float64Counter
	SetTotal          metric.Float64Counter
	CBOpen            metric.Float64Counter
	CBTooManyRequests metric.Float64Counter
	CBState           metric.Float64Gauge
	Duration          metric.Float64Histogram
}

StatsOTEL holds OpenTelemetry counters for cache hit, miss, and set operations.

func GetStatsOTEL

func GetStatsOTEL(name string) (*StatsOTEL, error)

GetStatsOTEL creates a new StatsOTEL instance with OpenTelemetry counters for the given meter name.

type StatsProm

type StatsProm struct {
	HitsLocal         *prometheus.CounterVec
	MissesLocal       *prometheus.CounterVec
	HitsRemote        *prometheus.CounterVec
	MissesRemote      *prometheus.CounterVec
	HitsStale         *prometheus.CounterVec
	SetLocal          *prometheus.CounterVec
	SetRemote         *prometheus.CounterVec
	SetTotal          *prometheus.CounterVec
	CBOpen            *prometheus.CounterVec
	CBTooManyRequests *prometheus.CounterVec
	CBState           *prometheus.GaugeVec
	Duration          *prometheus.HistogramVec
}

StatsProm contains accumulated stats.

func GetStatsProm

func GetStatsProm(metricNamespace, metricSubSystem string) *StatsProm

GetStatsProm creates a new StatsProm instance with Prometheus counter vectors for the given namespace and subsystem.

type TinyLFU

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

TinyLFU is a concurrency-safe local cache using the TinyLFU admission policy.

func NewTinyLFU

func NewTinyLFU(size int, ttl time.Duration) *TinyLFU

NewTinyLFU creates a TinyLFU cache. size is the maximum number of items.

func (*TinyLFU) Del

func (c *TinyLFU) Del(key []byte)

Del removes a key from the cache.

func (*TinyLFU) Get

func (c *TinyLFU) Get(key []byte) ([]byte, error)

Get retrieves a value by key from the cache.

func (*TinyLFU) Set

func (c *TinyLFU) Set(key []byte, data []byte) error

Set stores a key-value pair in the cache using the default TTL.

func (*TinyLFU) SetExp

func (c *TinyLFU) SetExp(key []byte, data []byte, ttl time.Duration) error

SetExp stores a key-value pair in the cache with a custom TTL.

Jump to

Keyboard shortcuts

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