cachex

package
v0.10.8 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HybridCache

type HybridCache[V any] struct {
	// contains filtered or unexported fields
}

HybridCache is a small helper that uses Redis when enabled, otherwise falls back to in-memory hot cache.

func NewHybridCache

func NewHybridCache[V any](cfg HybridCacheConfig[V]) *HybridCache[V]

func (*HybridCache[V]) Algorithm

func (c *HybridCache[V]) Algorithm() (mainCacheAlgorithm string, missingCacheAlgorithm string)

func (*HybridCache[V]) Capacity

func (c *HybridCache[V]) Capacity() (mainCacheCapacity int, missingCacheCapacity int)

func (*HybridCache[V]) DeleteByPrefix

func (c *HybridCache[V]) DeleteByPrefix(prefix string) (int, error)

func (*HybridCache[V]) DeleteMany

func (c *HybridCache[V]) DeleteMany(keys []string) (map[string]bool, error)

DeleteMany accepts either fully namespaced keys or raw keys and deletes them. It returns a map keyed by fully namespaced keys.

func (*HybridCache[V]) FullKey

func (c *HybridCache[V]) FullKey(key string) string

func (*HybridCache[V]) Get

func (c *HybridCache[V]) Get(key string) (value V, found bool, err error)

func (*HybridCache[V]) Keys

func (c *HybridCache[V]) Keys() ([]string, error)

Keys returns keys with valid values. In Redis, it returns all matching keys.

func (*HybridCache[V]) Purge

func (c *HybridCache[V]) Purge() error

func (*HybridCache[V]) SetWithTTL

func (c *HybridCache[V]) SetWithTTL(key string, v V, ttl time.Duration) error

type HybridCacheConfig

type HybridCacheConfig[V any] struct {
	Namespace Namespace

	// Redis is used when RedisEnabled returns true (or RedisEnabled is nil) and Redis is not nil.
	Redis        *redis.Client
	RedisCodec   ValueCodec[V]
	RedisEnabled func() bool

	// Memory builds a hot cache used when Redis is disabled. Keys stored in memory are fully namespaced.
	Memory func() *hot.HotCache[string, V]
}

type IntCodec

type IntCodec struct{}

func (IntCodec) Decode

func (c IntCodec) Decode(s string) (int, error)

func (IntCodec) Encode

func (c IntCodec) Encode(v int) (string, error)

type JSONCodec

type JSONCodec[V any] struct{}

func (JSONCodec[V]) Decode

func (c JSONCodec[V]) Decode(s string) (V, error)

func (JSONCodec[V]) Encode

func (c JSONCodec[V]) Encode(v V) (string, error)

type Namespace

type Namespace string

Namespace isolates keys between different cache use-cases. (e.g. "channel_affinity:v1").

func (Namespace) FullKey

func (n Namespace) FullKey(key string) string

func (Namespace) MatchPattern

func (n Namespace) MatchPattern() string

type StringCodec

type StringCodec struct{}

func (StringCodec) Decode

func (c StringCodec) Decode(s string) (string, error)

func (StringCodec) Encode

func (c StringCodec) Encode(v string) (string, error)

type ValueCodec

type ValueCodec[V any] interface {
	Encode(v V) (string, error)
	Decode(s string) (V, error)
}

Jump to

Keyboard shortcuts

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