lru

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: Apache-2.0 Imports: 13 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.Response]) *Balance

NewBalancer creates a new Balance instance and initializes memList.

func (*Balance) FindVictim

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

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 *list2.Element[*model.Response])

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.Response])

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 *list2.Element[*model.Response])

func (*Balance) Set

func (b *Balance) Set(resp *model.Response)

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.Response)

type Balancer

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

type ShardNode

type ShardNode struct {
	Shard *sharded.Shard[*model.Response] // 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() *list2.List[*model.Response]

func (*ShardNode) RandItem

func (s *ShardNode) RandItem(ctx context.Context) *model.Response

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.Response],
) *Storage

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

func (*Storage) Get

func (s *Storage) Get(req *model.Request) (*model.Response, bool)

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

func (*Storage) GetRandom added in v0.9.2

func (s *Storage) GetRandom() (resp *model.Response, isFound bool)

func (*Storage) Mem

func (s *Storage) Mem() int64

func (*Storage) RealMem

func (s *Storage) RealMem() int64

func (*Storage) Remove

func (s *Storage) Remove(resp *model.Response) (freedBytes int64, isHit bool)

func (*Storage) Run

func (s *Storage) Run()

func (*Storage) Set

func (s *Storage) Set(new *model.Response)

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