lru

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 15 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 LRUStorage 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.Entry]) *Balance

NewBalancer creates a new Balance instance and initializes memList.

func (*Balance) FindVictim

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

func (*Balance) Mem added in v0.9.3

func (b *Balance) Mem() int64

func (*Balance) MostLoaded added in v1.0.1

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

MostLoaded 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) Push added in v1.0.1

func (b *Balance) Push(entry *model.Entry)

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

func (*Balance) Rebalance

func (b *Balance) Rebalance()

func (*Balance) Register

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

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

func (*Balance) Remove

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

func (*Balance) Update

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

type Balancer

type Balancer interface {
	Rebalance()
	Mem() int64
	Register(shard *sharded.Shard[*model.Entry])
	Push(entry *model.Entry)
	Update(existing *model.Entry)
	Remove(shardKey uint64, el *list.Element[*model.Entry])
	MostLoaded(offset int) (*ShardNode, bool)
	FindVictim(shardKey uint64) (*model.Entry, bool)
}

type Evict added in v0.9.8

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

func NewEvictor added in v0.9.8

func NewEvictor(ctx context.Context, cfg *config.Cache, db *InMemoryStorage, balancer *Balance) *Evict

func (*Evict) Run added in v0.9.8

func (e *Evict) Run() *Evict

Run is the main background eviction loop for one worker. Each worker tries to bring Weight usage under the threshold by evicting from most loaded shards.

func (*Evict) ShouldEvict added in v0.9.8

func (e *Evict) ShouldEvict() bool

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

type EvictionStat added in v0.9.8

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

EvictionStat carries statistics for each eviction batch.

type Evictor added in v0.9.8

type Evictor interface {
	// contains filtered or unexported methods
}

type InMemoryStorage added in v1.0.1

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

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

func NewStorage

func NewStorage(ctx context.Context, cfg *config.Cache, backend repository.Backender) *InMemoryStorage

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

func (*InMemoryStorage) Clear added in v1.0.1

func (s *InMemoryStorage) Clear()

func (*InMemoryStorage) Get added in v1.0.1

func (s *InMemoryStorage) Get(req *model.Entry) (ptr *model.Entry, found bool)

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

func (*InMemoryStorage) Len added in v1.0.1

func (s *InMemoryStorage) Len() int64

func (*InMemoryStorage) Mem added in v1.0.1

func (s *InMemoryStorage) Mem() int64

func (*InMemoryStorage) Rand added in v1.0.1

func (s *InMemoryStorage) Rand() (entry *model.Entry, ok bool)

Rand returns a random item from storage.

func (*InMemoryStorage) RealLen added in v1.0.1

func (s *InMemoryStorage) RealLen() int64

func (*InMemoryStorage) RealMem added in v1.0.1

func (s *InMemoryStorage) RealMem() int64

func (*InMemoryStorage) Remove added in v1.0.1

func (s *InMemoryStorage) Remove(entry *model.Entry) (freedBytes int64, hit bool)

func (*InMemoryStorage) Set added in v1.0.1

func (s *InMemoryStorage) Set(new *model.Entry) (persisted bool)

Set inserts or updates a response in the cache, updating Weight usage and InMemoryStorage position. On 'wasPersisted=true' must be called Entry.Finalize, otherwise Entry.Finalize.

func (*InMemoryStorage) ShouldEvict added in v1.0.1

func (s *InMemoryStorage) ShouldEvict() bool

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

func (*InMemoryStorage) Stat added in v1.0.1

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

func (*InMemoryStorage) WalkShards added in v1.0.1

func (s *InMemoryStorage) WalkShards(ctx context.Context, fn func(key uint64, shard *sharded.Shard[*model.Entry]))

type Refresh added in v1.0.1

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

Refresh is responsible for background refreshing of cache entries. It periodically samples random shards and randomly selects "cold" entries (from the end of each shard's InMemoryStorage list) to refreshItem if necessary. Communication: provider->consumer (MPSC).

func NewRefresher added in v1.0.1

func NewRefresher(ctx context.Context, cfg *config.Cache, storage *InMemoryStorage) *Refresh

NewRefresher constructs a Refresh.

func (*Refresh) Run added in v1.0.1

func (r *Refresh) Run() *Refresh

Run starts the refresher background loop. It runs a logger (if debugging is enabled), spawns a provider for sampling shards, and continuously processes shard samples for candidate responses to refreshItem.

type Refresher added in v1.0.1

type Refresher interface {
	// contains filtered or unexported methods
}

type ShardNode

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

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

func (*ShardNode) LruList

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

func (*ShardNode) Weight

func (s *ShardNode) Weight() int64

Weight returns an approximate Weight usage of this ShardNode structure.

Jump to

Keyboard shortcuts

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