minder

package module
v0.0.0-...-ab64891 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

README

MemoryCache

thread-safe cache with auto-delete

Documentation

Index

Constants

View Source
const (
	LFU_SHARD_COUNT = 512
	LFU_TOTAL       = 1024_000
)
View Source
const SHARD_COUNT = 1024
View Source
const SHARD_MASK = SHARD_COUNT - 1
View Source
const TOTAL = 1024_000

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
}

func NewCache

func NewCache[K comparable, V any](size ...int) *Cache[K, V]

Size optional

func (*Cache[K, V]) Cap

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

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

func (*Cache[K, V]) Close

func (c *Cache[K, V]) Close()

func (*Cache[K, V]) Del

func (c *Cache[K, V]) Del(k K)

func (*Cache[K, V]) ExpiredCount

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

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(k K) (V, bool)

func (*Cache[K, V]) GetTTL

func (c *Cache[K, V]) GetTTL(k K) (time.Duration, bool)

func (*Cache[K, V]) Len

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

func (*Cache[K, V]) Range

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

func (*Cache[K, V]) RangeKeys

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

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(k K, v V) bool

func (*Cache[K, V]) SetOrGet

func (c *Cache[K, V]) SetOrGet(k K, v V) bool

func (*Cache[K, V]) SetWithTTL

func (c *Cache[K, V]) SetWithTTL(k K, v V, ttl time.Duration) bool

func (*Cache[K, V]) Size

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

type ShardedCache

type ShardedCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewShardedCache

func NewShardedCache[K comparable, V any]() *ShardedCache[K, V]

func (*ShardedCache[K, V]) Clear

func (sc *ShardedCache[K, V]) Clear()

func (*ShardedCache[K, V]) Close

func (sc *ShardedCache[K, V]) Close()

func (*ShardedCache[K, V]) Del

func (sc *ShardedCache[K, V]) Del(k K)

func (*ShardedCache[K, V]) Get

func (sc *ShardedCache[K, V]) Get(k K) (V, bool)

func (*ShardedCache[K, V]) GetTTL

func (sc *ShardedCache[K, V]) GetTTL(k K) (time.Duration, bool)

func (*ShardedCache[K, V]) Len

func (sc *ShardedCache[K, V]) Len() int

func (*ShardedCache[K, V]) Range

func (sc *ShardedCache[K, V]) Range(f func(key K, value V) bool)

Range iterates over all items sequentially. The callback is called from a single goroutine, so no external synchronization needed. Return false from callback to stop iteration.

func (*ShardedCache[K, V]) RangeParallel

func (sc *ShardedCache[K, V]) RangeParallel(f func(key K, value V) bool)

RangeParallel iterates over all items in parallel across shards. WARNING: The callback may be called from multiple goroutines simultaneously. Ensure your callback is thread-safe or use Range() for sequential iteration.

func (*ShardedCache[K, V]) Set

func (sc *ShardedCache[K, V]) Set(k K, v V) bool

func (*ShardedCache[K, V]) SetWithTTL

func (sc *ShardedCache[K, V]) SetWithTTL(k K, v V, ttl time.Duration) bool

type ShardedLFUCache

type ShardedLFUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ShardedLFUCache is a sharded cache with TinyLFU-based eviction.

func NewShardedLFUCache

func NewShardedLFUCache[K comparable, V any]() *ShardedLFUCache[K, V]

NewShardedLFUCache creates a new sharded cache with LFU eviction.

func NewShardedLFUCacheWithSize

func NewShardedLFUCacheWithSize[K comparable, V any](totalSize int64) *ShardedLFUCache[K, V]

NewShardedLFUCacheWithSize creates a new sharded LFU cache with specified total capacity.

func (*ShardedLFUCache[K, V]) Cap

func (sc *ShardedLFUCache[K, V]) Cap() int64

Cap returns the remaining capacity across all shards.

func (*ShardedLFUCache[K, V]) Clear

func (sc *ShardedLFUCache[K, V]) Clear()

Clear removes all items from the cache.

func (*ShardedLFUCache[K, V]) Close

func (sc *ShardedLFUCache[K, V]) Close()

Close stops the cache cleanup routine and releases resources.

func (*ShardedLFUCache[K, V]) Del

func (sc *ShardedLFUCache[K, V]) Del(k K)

Del removes an item from the cache.

func (*ShardedLFUCache[K, V]) FlushAccessBuffers

func (sc *ShardedLFUCache[K, V]) FlushAccessBuffers()

FlushAccessBuffers forces all shards to process pending access buffers. Useful for testing when you need immediate frequency updates.

func (*ShardedLFUCache[K, V]) Get

func (sc *ShardedLFUCache[K, V]) Get(k K) (V, bool)

Get retrieves an item from the cache.

func (*ShardedLFUCache[K, V]) GetTTL

func (sc *ShardedLFUCache[K, V]) GetTTL(k K) (time.Duration, bool)

GetTTL returns the remaining TTL for an item.

func (*ShardedLFUCache[K, V]) Len

func (sc *ShardedLFUCache[K, V]) Len() int

Len returns the total number of valid items in the cache.

func (*ShardedLFUCache[K, V]) MaxCost

func (sc *ShardedLFUCache[K, V]) MaxCost() int64

MaxCost returns the maximum cost capacity across all shards.

func (*ShardedLFUCache[K, V]) Range

func (sc *ShardedLFUCache[K, V]) Range(f func(key K, value V) bool)

Range iterates over all items sequentially. The callback is called from a single goroutine, so no external synchronization needed. Return false from callback to stop iteration.

func (*ShardedLFUCache[K, V]) RangeParallel

func (sc *ShardedLFUCache[K, V]) RangeParallel(f func(key K, value V) bool)

RangeParallel iterates over all items in parallel across shards. WARNING: The callback may be called from multiple goroutines simultaneously. Ensure your callback is thread-safe or use Range() for sequential iteration.

func (*ShardedLFUCache[K, V]) Set

func (sc *ShardedLFUCache[K, V]) Set(k K, v V) bool

Set adds an item to the cache with default cost of 1.

func (*ShardedLFUCache[K, V]) SetWithCost

func (sc *ShardedLFUCache[K, V]) SetWithCost(k K, v V, cost int64) bool

SetWithCost adds an item with a specified cost.

func (*ShardedLFUCache[K, V]) SetWithTTL

func (sc *ShardedLFUCache[K, V]) SetWithTTL(k K, v V, ttl time.Duration) bool

SetWithTTL adds an item with a TTL and default cost of 1.

func (*ShardedLFUCache[K, V]) SetWithTTLAndCost

func (sc *ShardedLFUCache[K, V]) SetWithTTLAndCost(k K, v V, ttl time.Duration, cost int64) bool

SetWithTTLAndCost adds an item with both TTL and cost.

type UserLFUCache

type UserLFUCache[U comparable, K comparable, V any] struct {
	// contains filtered or unexported fields
}

UserLFUCache is a cache with per-user record limits and LFU eviction

func NewUserLFUCache

func NewUserLFUCache[U comparable, K comparable, V any](config UserLFUCacheConfig) *UserLFUCache[U, K, V]

NewUserLFUCache creates a new cache with per-user limits and LFU eviction

func (*UserLFUCache[U, K, V]) Clear

func (c *UserLFUCache[U, K, V]) Clear()

Clear removes all cached records

func (*UserLFUCache[U, K, V]) Close

func (c *UserLFUCache[U, K, V]) Close()

Close stops the cleanup routine

func (*UserLFUCache[U, K, V]) Config

func (c *UserLFUCache[U, K, V]) Config() UserLFUCacheConfig

Config returns the cache configuration

func (*UserLFUCache[U, K, V]) Del

func (c *UserLFUCache[U, K, V]) Del(userID U, key K)

Del removes a record

func (*UserLFUCache[U, K, V]) DelUser

func (c *UserLFUCache[U, K, V]) DelUser(userID U)

DelUser removes all records for a user

func (*UserLFUCache[U, K, V]) Flush

func (c *UserLFUCache[U, K, V]) Flush()

Flush forces processing of all buffered LFU access updates. Intended for testing only.

func (*UserLFUCache[U, K, V]) ForceSweep

func (c *UserLFUCache[U, K, V]) ForceSweep()

ForceSweep runs cleanup immediately (for testing)

func (*UserLFUCache[U, K, V]) Get

func (c *UserLFUCache[U, K, V]) Get(userID U, key K) (V, bool)

Get retrieves a record and updates access frequency

func (*UserLFUCache[U, K, V]) GetAllSorted

func (c *UserLFUCache[U, K, V]) GetAllSorted(userID U) []UserRecord[K, V]

GetAllSorted returns all records for a user sorted by createdAt (newest first)

func (*UserLFUCache[U, K, V]) GetByKey

func (c *UserLFUCache[U, K, V]) GetByKey(k K) (V, bool)

GetByKey retrieves a record by key only (without knowing userID). Uses globalKeyIdx for O(1) shard lookup.

func (*UserLFUCache[U, K, V]) GetNewest

func (c *UserLFUCache[U, K, V]) GetNewest(userID U, count int) []UserRecord[K, V]

GetNewest returns the N newest records for a user (sorted by createdAt, newest first) This is the main method for getting DEFAULT_CACHED_COUNT newest records

func (*UserLFUCache[U, K, V]) GetUserRecordCount

func (c *UserLFUCache[U, K, V]) GetUserRecordCount(userID U) int

GetUserRecordCount returns the number of cached records for a user

func (*UserLFUCache[U, K, V]) IsUserExpanded

func (c *UserLFUCache[U, K, V]) IsUserExpanded(userID U) bool

IsUserExpanded returns whether the user has requested full history

func (*UserLFUCache[U, K, V]) Len

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

Len returns total number of cached records (O(shards)

func (*UserLFUCache[U, K, V]) LoadUserRecords

func (c *UserLFUCache[U, K, V]) LoadUserRecords(userID U, items []struct {
	Key       K
	Value     V
	CreatedAt time.Time
	Cost      int64
}) int

LoadUserRecords loads multiple records for a user at once (for initial load from DB) All records get the same cached timestamp

func (*UserLFUCache[U, K, V]) Range

func (c *UserLFUCache[U, K, V]) Range(f func(userID U, key K, value V) bool)

Range iterates over all records in the cache. The callback receives userID, key, value for each record. Return false from the callback to stop iteration. Each accessed item has its frequency incremented.

func (*UserLFUCache[U, K, V]) RangeByUser

func (c *UserLFUCache[U, K, V]) RangeByUser(userID U, f func(key K, value V) bool)

RangeByUser iterates over all records for a specific user. Return false from the callback to stop iteration. Each accessed item has its frequency incremented.

func (*UserLFUCache[U, K, V]) RangeRecords

func (c *UserLFUCache[U, K, V]) RangeRecords(f func(userID U, record UserRecord[K, V]) bool)

RangeRecords iterates over all records with full metadata. The callback receives userID and UserRecord for each record. Return false from the callback to stop iteration. Each accessed item has its frequency incremented.

func (*UserLFUCache[U, K, V]) RangeUsers

func (c *UserLFUCache[U, K, V]) RangeUsers(f func(userID U, recordCount int) bool)

RangeUsers iterates over all users in the cache. The callback receives userID and record count for each user. Return false from the callback to stop iteration.

func (*UserLFUCache[U, K, V]) ReplaceUserRecords

func (c *UserLFUCache[U, K, V]) ReplaceUserRecords(userID U, items []struct {
	Key       K
	Value     V
	CreatedAt time.Time
	Cost      int64
})

ReplaceUserRecords replaces all records for a user (for full history load) All records get the same cached timestamp to preserve relative age

func (*UserLFUCache[U, K, V]) Set

func (c *UserLFUCache[U, K, V]) Set(userID U, key K, value V, createdAt time.Time) bool

Set adds or updates a record for a user createdAt - time created item (for sort)

func (*UserLFUCache[U, K, V]) SetUserExpanded

func (c *UserLFUCache[U, K, V]) SetUserExpanded(userID U, expanded bool)

SetUserExpanded marks a user as having full history loaded

func (*UserLFUCache[U, K, V]) SetWithCost

func (c *UserLFUCache[U, K, V]) SetWithCost(userID U, key K, value V, createdAt time.Time, cost int64) bool

SetWithCost adds or updates a record with specified cost

func (*UserLFUCache[U, K, V]) UserCount

func (c *UserLFUCache[U, K, V]) UserCount() int

UserCount returns number of users with cached records

type UserLFUCacheConfig

type UserLFUCacheConfig struct {
	// DefaultCachedCount - count cache items for user
	DefaultCachedCount int

	// SweepInterval - interval for sweep clean
	SweepInterval time.Duration

	// CacheAgeDays - max time for cache item(ttl)
	CacheAgeDays int

	// TotalCapacity - capacity for lfu evictions
	TotalCapacity int64

	// ShardCount - shard count,must be power of t2o
	ShardCount int
}

Config for UserLFUCache

func DefaultUserLFUConfig

func DefaultUserLFUConfig() UserLFUCacheConfig

DefaultUserLFUConfig returns default configuration

type UserRecord

type UserRecord[K comparable, V any] struct {
	Key       K
	Value     V
	CreatedAt time.Time
	CachedAt  time.Time
}

UserRecord represents a cached record with metadata

Jump to

Keyboard shortcuts

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