lru

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balance

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

Balance maintains per-Shard Storage lists and provides efficient selection of loaded shards for eviction. - memList orders shardNodes by usage (most loaded in front). - shards is a flat array for O(1) access by Shard index. - shardedMap is the underlying data storage (map of all entries).

func NewBalancer

func NewBalancer(ctx context.Context, shardedMap *sharded.Map[*model.VersionPointer]) *Balance

NewBalancer creates a new Balance instance and initializes memList.

func (*Balance) FindVictim

func (b *Balance) FindVictim(shardKey uint64) (*model.VersionPointer, bool)

func (*Balance) Mem added in v0.9.3

func (b *Balance) Mem() int64

func (*Balance) MostLoadedSampled

func (b *Balance) MostLoadedSampled(offset int) (*ShardNode, bool)

MostLoadedSampled returns the first non-empty Shard node from the front of memList, optionally skipping a number of nodes by offset (for concurrent eviction fairness).

func (*Balance) Move

func (b *Balance) Move(shardKey uint64, el *list.Element[*model.VersionPointer])

Move moves an element to the front of the per-Shard Storage list. Used for touch/Set operations to mark entries as most recently used.

func (*Balance) RandNode

func (b *Balance) RandNode() *ShardNode

RandNode returns a random ShardNode for sampling (e.g., for background refreshers).

func (*Balance) Rebalance

func (b *Balance) Rebalance()

func (*Balance) Register

func (b *Balance) Register(shard *sharded.Shard[*model.VersionPointer])

Register inserts a new ShardNode for a given Shard, creates its Storage, and adds it to memList and shards array.

func (*Balance) Remove

func (b *Balance) Remove(shardKey uint64, el *list.Element[*model.VersionPointer])

func (*Balance) Set

func (b *Balance) Set(entry *model.VersionPointer)

Set inserts a response into the appropriate Shard's Storage list and updates counters. Returns the affected ShardNode for further operations.

func (*Balance) Shards

func (b *Balance) Shards() [sharded.NumOfShards]*ShardNode

func (*Balance) Update

func (b *Balance) Update(existing *model.VersionPointer)

type Balancer

type Balancer interface {
	Rebalance()
	Mem() int64
	Shards() [sharded.NumOfShards]*ShardNode
	RandNode() *ShardNode
	Register(shard *sharded.Shard[*model.VersionPointer])
	Set(entry *model.VersionPointer)
	Update(existing *model.VersionPointer)
	Move(shardKey uint64, el *list.Element[*model.VersionPointer])
	Remove(shardKey uint64, el *list.Element[*model.VersionPointer])
	MostLoadedSampled(offset int) (*ShardNode, bool)
	FindVictim(shardKey uint64) (*model.VersionPointer, bool)
}

type ShardNode

type ShardNode struct {
	Shard *sharded.Shard[*model.VersionPointer] // Reference to the actual Shard (map + sync)
	// contains filtered or unexported fields
}

ShardNode represents a single Shard's Storage and accounting info. Each Shard has its own Storage list and a pointer to its element in the balancer's memList.

func (*ShardNode) LruList

func (s *ShardNode) LruList() *list.List[*model.VersionPointer]

func (*ShardNode) RandItem

func (s *ShardNode) RandItem() (*model.VersionPointer, bool)

func (*ShardNode) Weight

func (s *ShardNode) Weight() int64

Weight returns an approximate Weight usage of this ShardNode structure.

type Storage

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

Storage is a Weight-aware, sharded Storage cache with background eviction and refreshItem support.

func NewStorage

func NewStorage(
	ctx context.Context,
	cfg *config.Cache,
	balancer Balancer,
	backend repository.Backender,
	tinyLFU *lfu.TinyLFU,
	shardedMap *sharded.Map[*model.VersionPointer],
) *Storage

NewStorage constructs a new Storage cache instance and launches eviction and refreshItem routines.

func (*Storage) Clear added in v0.9.7

func (s *Storage) Clear()

func (*Storage) Get

func (s *Storage) Get(req *model.Entry) (entry *model.VersionPointer, ok bool)

Get retrieves a response by request and bumps its Storage position. Returns: (response, releaser, found).

func (*Storage) GetRand added in v0.9.3

func (s *Storage) GetRand() (entry *model.VersionPointer, ok bool)

GetRand returns a random item from storage.

func (*Storage) Len added in v0.9.3

func (s *Storage) Len() int64

func (*Storage) Mem

func (s *Storage) Mem() int64

func (*Storage) RealMem

func (s *Storage) RealMem() int64

func (*Storage) Remove

func (s *Storage) Remove(entry *model.VersionPointer) (freedBytes int64, isHit bool)

func (*Storage) Run

func (s *Storage) Run()

func (*Storage) Set

func (s *Storage) Set(new *model.VersionPointer) (entry *model.VersionPointer)

Set inserts or updates a response in the cache, updating Weight usage and Storage position.

func (*Storage) ShouldEvict

func (s *Storage) ShouldEvict() bool

ShouldEvict [HOT PATH METHOD] (max stale value = 25ms) checks if current Weight usage has reached or exceeded the threshold.

func (*Storage) Stat

func (s *Storage) Stat() (bytes int64, length int64)

Jump to

Keyboard shortcuts

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