lru

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ShardLRU

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

ShardLRU 是一个分片式的 LRU 缓存

func NewShardLRU

func NewShardLRU[K comparable, V any](options ...ShardLRUOption[K, V]) *ShardLRU[K, V]

NewShardLRU 创建一个新的分片式 LRU 缓存 shardCount: 分片数量,默认为16,会向上取整为2的幂 capacity: 总容量,会平均分配给所有分片

func (*ShardLRU[K, V]) Clear

func (lru *ShardLRU[K, V]) Clear()

Clear 清空缓存

func (*ShardLRU[K, V]) Contains

func (lru *ShardLRU[K, V]) Contains(key K) bool

Contains 检查键是否存在

func (*ShardLRU[K, V]) Delete

func (lru *ShardLRU[K, V]) Delete(key K) bool

func (*ShardLRU[K, V]) Get

func (lru *ShardLRU[K, V]) Get(key K) (V, bool)

func (*ShardLRU[K, V]) Len

func (lru *ShardLRU[K, V]) Len() int

Len 返回当前缓存中的项目数

func (*ShardLRU[K, V]) Set

func (lru *ShardLRU[K, V]) Set(key K, value V)

func (*ShardLRU[K, V]) Stats

func (lru *ShardLRU[K, V]) Stats() (hitRate float64, shardLoad []float64)

Stats 返回缓存统计信息,包括命中率和每个分片的负载

type ShardLRUOption

type ShardLRUOption[K comparable, V any] func(*ShardLRU[K, V])

func WithCapacity

func WithCapacity[K comparable, V any](capacity int) ShardLRUOption[K, V]

func WithLRUOnEvict

func WithLRUOnEvict[K comparable, V any](onEvict func(K, V)) ShardLRUOption[K, V]

func WithShardCount

func WithShardCount[K comparable, V any](shardCount int) ShardLRUOption[K, V]

type SimpleLRU added in v1.0.2

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

func NewSimpleLRU added in v1.0.2

func NewSimpleLRU[K comparable, V any](capacity int, onEvict func(K, V)) *SimpleLRU[K, V]

func (*SimpleLRU[K, V]) Capacity added in v1.0.2

func (lru *SimpleLRU[K, V]) Capacity() int

func (*SimpleLRU[K, V]) Clear added in v1.0.2

func (lru *SimpleLRU[K, V]) Clear()

func (*SimpleLRU[K, V]) Contains added in v1.0.2

func (lru *SimpleLRU[K, V]) Contains(key K) bool

Contains 检查键是否存在(不更新访问顺序)

func (*SimpleLRU[K, V]) Delete added in v1.0.2

func (lru *SimpleLRU[K, V]) Delete(key K) bool

Delete 删除键值对

func (*SimpleLRU[K, V]) Get added in v1.0.2

func (lru *SimpleLRU[K, V]) Get(key K) (V, bool)

Get 获取键对应的值,如果存在则将节点移到链表头部

func (*SimpleLRU[K, V]) Len added in v1.0.2

func (lru *SimpleLRU[K, V]) Len() int

Len 返回当前缓存中的项目数(别名方法)

func (*SimpleLRU[K, V]) OnEvict added in v1.0.2

func (lru *SimpleLRU[K, V]) OnEvict() func(K, V)

func (*SimpleLRU[K, V]) Set added in v1.0.2

func (lru *SimpleLRU[K, V]) Set(key K, value V)

Set 设置键值对,如果键已存在则更新值并移到头部

func (*SimpleLRU[K, V]) SetOnEvict added in v1.0.2

func (lru *SimpleLRU[K, V]) SetOnEvict(onEvict func(K, V))

func (*SimpleLRU[K, V]) Size added in v1.0.2

func (lru *SimpleLRU[K, V]) Size() int

Jump to

Keyboard shortcuts

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