Documentation
¶
Overview ¶
Package cache 提供支持容量、权重和淘汰保护的并发安全缓存。
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU 是一个并发安全的最近最少使用缓存。 缓存同时受条目数量和可选总权重限制;条目淘汰后会在缓存锁释放后调用 onEvict。
func NewLRU ¶
func NewLRU[K comparable, V any](capacity int, onEvict func(K, V)) *LRU[K, V]
NewLRU 创建只按条目数量限制的 LRU 缓存。
func NewWeightedLRU ¶
func NewWeightedLRU[K comparable, V any](capacity int, maxWeight int64, onEvict func(K, V), canEvict ...func(K, V) bool) *LRU[K, V]
NewWeightedLRU 创建按条目数量和总权重限制的 LRU 缓存。 maxWeight 小于等于 0 表示不启用权重限制。可选的 canEvict 只用于暂时保护条目。
func (*LRU[K, V]) AddWeighted ¶
AddWeighted 添加一个带权重的条目;负权重按 0 处理。
func (*LRU[K, V]) Trim ¶
func (c *LRU[K, V]) Trim()
Trim 主动按当前容量和权重限制清理可淘汰条目。 被淘汰策略保护的条目会暂时保留,待之后再次调用 Trim 时处理。
Click to show internal directories.
Click to hide internal directories.