cache

package
v1.8.8-0...-edaca3c Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cache contains in-process MRU/L1 cache implementations and utilities used by SOP. It offers a generic Cache interface and concrete MRU- and L1-based caches.

Package cache contains the L1 (MRU) cache implementation and L2 integration for B-tree nodes and handles.

Index

Constants

This section is empty.

Variables

View Source
var DefaultInMemoryCacheShardCapacity = 1000

DefaultInMemoryCacheShardCapacity is the default per-shard entry ceiling for the in-memory L2 cache. In standalone mode, L1 is intentionally smaller while L2 carries the broader hot-node working set. With 256 shards, this provides a total capacity of 256,000 entries (1000 × 256).

View Source
var DefaultMaxCapacity = 512

DefaultMaxCapacity is the default hard limit of entries allowed in the L1 cache.

View Source
var DefaultMinCapacity = 256

DefaultMinCapacity is the default minimum number of entries to retain before evictions are considered. Clustered deployments benefit from a larger L1 footprint because L2 (Redis) access has network latency.

View Source
var DefaultStandaloneMaxCapacity = 64

DefaultStandaloneMaxCapacity is the L1 cache ceiling for standalone/in-process usage.

View Source
var DefaultStandaloneMinCapacity = 32

DefaultStandaloneMinCapacity is the L1 cache footprint for standalone/in-process usage. Standalone uses a smaller L1 because L2 is also in-memory in the same process (deserialization only, no network).

Functions

func FormatNodeKey

func FormatNodeKey(k string) string

FormatNodeKey prefixes the key with 'N' to form the cache key for a node.

func NewL2InMemoryCache

func NewL2InMemoryCache() sop.L2Cache

func NewStandaloneL2InMemoryCache

func NewStandaloneL2InMemoryCache() sop.L2Cache

NewStandaloneL2InMemoryCache creates an in-memory L2 cache suitable for standalone processes.

Types

type Cache

type Cache[TK comparable, TV any] interface {
	// Clear removes all entries from the cache.
	Clear()
	// Set inserts or updates the given key/value pairs.
	Set(items []sop.KeyValuePair[TK, TV])
	// Get looks up the values for the given keys; missing keys yield zero values.
	Get(keys []TK) []TV
	// Delete removes the given keys from the cache, if present.
	Delete(keys []TK)
	// Count returns the number of items currently stored in the cache.
	Count() int
	// IsFull reports whether the cache has reached its maximum capacity.
	IsFull() bool
	// Evict removes least-recently-used entries until capacity constraints are satisfied.
	Evict()
}

Cache is a generic MRU cache interface used for in-memory caching scenarios. Implementations should maintain recency and support bulk operations.

func NewCache

func NewCache[TK comparable, TV any](minCapacity, maxCapacity int) Cache[TK, TV]

NewCache creates a new generic cache with MRU-based eviction.

func NewSynchronizedCache

func NewSynchronizedCache[TK comparable, TV any](minCapacity, maxCapacity int) Cache[TK, TV]

NewSynchronizedCache returns a thread-safe Cache instance backed by an MRU cache.

type L1Cache

type L1Cache struct {
	Handles Cache[sop.UUID, sop.Handle]
	// contains filtered or unexported fields
}

L1Cache is an in-memory MRU cache for B-tree nodes and handle objects. It optionally integrates with an L2 cache (e.g., Redis) for cross-process sharing and TTL.

func GetGlobalL1Cache

func GetGlobalL1Cache(l2c sop.L2Cache) *L1Cache

GetGlobalL1Cache returns the global L1 cache singleton, creating one on first use with the provided L2 cache and capacities appropriate for the deployment mode.

func NewL1Cache

func NewL1Cache(l2c sop.L2Cache, minCapacity, maxCapacity int) *L1Cache

NewL1Cache constructs a new L1Cache with the given L2 cache and capacity bounds.

func (*L1Cache) Count

func (c *L1Cache) Count() int

Count returns the number of entries currently stored in the L1 cache.

func (*L1Cache) DeleteNodes

func (c *L1Cache) DeleteNodes(ctx context.Context, nodesIDs []sop.UUID) (bool, error)

DeleteNodes removes the given node IDs from both the L1 MRU and the L2 cache. It returns true if any entries were removed and the last error encountered when deleting from L2.

func (*L1Cache) Evict

func (c *L1Cache) Evict()

Evict removes least-recently-used entries until the cache is within capacity.

func (*L1Cache) GetNode

func (c *L1Cache) GetNode(ctx context.Context, handle sop.Handle, nodeTarget any, isNodeCacheTTL bool, nodeCacheTTLDuration time.Duration) (any, error)

GetNode loads the node by handle either from L1 (if fresh) or from L2, honoring TTL semantics, and refreshes the entry in L1 before returning it.

func (*L1Cache) GetNodeFromMRU

func (c *L1Cache) GetNodeFromMRU(handle sop.Handle, nodeTarget any) any

GetNodeFromMRU returns the node from L1 if the cached version matches the handle version; otherwise it returns nil.

func (*L1Cache) IsFull

func (c *L1Cache) IsFull() bool

IsFull reports whether the L1 cache has reached its maximum capacity.

func (*L1Cache) SetNode

func (c *L1Cache) SetNode(ctx context.Context, nodeID sop.UUID, node any, nodeCacheDuration time.Duration)

SetNode caches the provided node in the L1 MRU and also in the L2 cache with the given duration.

func (*L1Cache) SetNodeToMRU

func (c *L1Cache) SetNodeToMRU(ctx context.Context, nodeID sop.UUID, node any, nodeCacheDuration time.Duration)

SetNodeToMRU caches the provided node only in the L1 MRU without touching the L2 cache.

type L2InMemoryCache

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

func (*L2InMemoryCache) Clear

func (c *L2InMemoryCache) Clear(ctx context.Context) error

func (*L2InMemoryCache) CreateLockKeys

func (c *L2InMemoryCache) CreateLockKeys(keys []string) []*sop.LockKey

func (*L2InMemoryCache) CreateLockKeysForIDs

func (c *L2InMemoryCache) CreateLockKeysForIDs(keys []sop.Tuple[string, sop.UUID]) []*sop.LockKey

func (*L2InMemoryCache) Delete

func (c *L2InMemoryCache) Delete(ctx context.Context, keys []string) (bool, error)

func (*L2InMemoryCache) DualLock

func (c *L2InMemoryCache) DualLock(ctx context.Context, duration time.Duration, lockKeys []*sop.LockKey) (bool, sop.UUID, error)

func (*L2InMemoryCache) FormatLockKey

func (c *L2InMemoryCache) FormatLockKey(k string) string

func (*L2InMemoryCache) Get

func (c *L2InMemoryCache) Get(ctx context.Context, key string) (bool, string, error)

func (*L2InMemoryCache) GetEx

func (c *L2InMemoryCache) GetEx(ctx context.Context, key string, expiration time.Duration) (bool, string, error)

func (*L2InMemoryCache) GetStruct

func (c *L2InMemoryCache) GetStruct(ctx context.Context, key string, target interface{}) (bool, error)

func (*L2InMemoryCache) GetStructEx

func (c *L2InMemoryCache) GetStructEx(ctx context.Context, key string, target interface{}, expiration time.Duration) (bool, error)

func (*L2InMemoryCache) GetStructs

func (c *L2InMemoryCache) GetStructs(ctx context.Context, keys []string, targets []interface{}, expiration time.Duration) ([]bool, error)

func (*L2InMemoryCache) GetType

func (c *L2InMemoryCache) GetType() sop.L2CacheType

Returns InMemoryCache as L2Cache type.

func (*L2InMemoryCache) Info

func (c *L2InMemoryCache) Info(ctx context.Context, section string) (string, error)

func (*L2InMemoryCache) IsLocked

func (c *L2InMemoryCache) IsLocked(ctx context.Context, lockKeys []*sop.LockKey) (bool, error)

func (*L2InMemoryCache) IsLockedByOthers

func (c *L2InMemoryCache) IsLockedByOthers(ctx context.Context, lockKeyNames []string) (bool, error)

func (*L2InMemoryCache) IsLockedByOthersTTL

func (c *L2InMemoryCache) IsLockedByOthersTTL(ctx context.Context, lockKeyNames []string, duration time.Duration) (bool, error)

func (*L2InMemoryCache) IsLockedTTL

func (c *L2InMemoryCache) IsLockedTTL(ctx context.Context, duration time.Duration, lockKeys []*sop.LockKey) (bool, error)

func (*L2InMemoryCache) IsRestarted

func (c *L2InMemoryCache) IsRestarted(ctx context.Context) bool

func (*L2InMemoryCache) Lock

func (c *L2InMemoryCache) Lock(ctx context.Context, duration time.Duration, lockKeys []*sop.LockKey) (bool, sop.UUID, error)

func (*L2InMemoryCache) Ping

func (c *L2InMemoryCache) Ping(ctx context.Context) error

func (*L2InMemoryCache) Set

func (c *L2InMemoryCache) Set(ctx context.Context, key string, value string, expiration time.Duration) error

func (*L2InMemoryCache) SetStruct

func (c *L2InMemoryCache) SetStruct(ctx context.Context, key string, value interface{}, expiration time.Duration) error

func (*L2InMemoryCache) SetStructs

func (c *L2InMemoryCache) SetStructs(ctx context.Context, keys []string, values []interface{}, expiration time.Duration) error

func (*L2InMemoryCache) Unlock

func (c *L2InMemoryCache) Unlock(ctx context.Context, lockKeys []*sop.LockKey) error

Jump to

Keyboard shortcuts

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