Documentation
¶
Overview ¶
Package cachex 提供自研高性能内存缓存库: 并发分片、精确 LRU 淘汰、独立 TTL 过期与 Singleflight 击穿防护, 与 errx / logx 生态打通,零第三方依赖。
Index ¶
- Constants
- func GetOrSet(ctx context.Context, c *Cache, key string, ttl time.Duration, ...) (any, error)
- type Cache
- func (c *Cache) Cleanup()
- func (c *Cache) Clear()
- func (c *Cache) Close()
- func (c *Cache) Delete(key string) bool
- func (c *Cache) Get(key string) (any, bool)
- func (c *Cache) Len() int
- func (c *Cache) Set(key string, value any)
- func (c *Cache) SetTTL(key string, value any, ttl time.Duration)
- func (c *Cache) Stats() Stats
- type CacheEvent
- type EventHook
- type EvictReason
- type Metrics
- type Option
- func WithCapacity(n int) Option
- func WithCleanupInterval(d time.Duration) Option
- func WithDefaultTTL(d time.Duration) Option
- func WithEventHook(h EventHook) Option
- func WithLRURefresh(interval int) Option
- func WithMetrics(m Metrics) Option
- func WithOnEvicted(fn func(key string, value any, reason EvictReason)) Option
- func WithShards(n int) Option
- func WithTraceHook(h TraceHook) Option
- type Stats
- type TraceAttr
- type TraceHook
Constants ¶
View Source
const ( // CodeInvalidConfig 配置非法。 CodeInvalidConfig errx.Code = "CACHEX_INVALID_CONFIG" )
错误码定义:cachex 各失败场景的错误码。
View Source
const Version = "v1.4.1"
Version 是当前库版本,与 git tag 保持一致。
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache 是自研内存缓存入口,持有分片、统计与生命周期。 所有方法并发安全,可在多个 goroutine 间共享。
type CacheEvent ¶
type CacheEvent struct {
// Action 操作类型:get_hit / get_miss / set / delete / clear / evict。
Action string
// Key 键名;Clear 等全局操作为空。
Key string
// Err 操作结果错误;nil 表示成功。
Err error
}
CacheEvent 描述一次缓存操作事件。
type EventHook ¶
type EventHook interface {
// OnCacheEvent 在缓存操作结束时调用。
OnCacheEvent(ctx context.Context, e CacheEvent)
}
EventHook 是可选事件钩子(默认 no-op),由 eventx 等外部适配器接入。
type EvictReason ¶
type EvictReason uint8
EvictReason 是条目被移除的原因。
const ( // EvictCapacity 容量超限,LRU 逐出。 EvictCapacity EvictReason = iota // EvictExpired 条目过期被清理。 EvictExpired )
type Option ¶
type Option func(*config)
Option 修改 Cache 配置,在 New 时按顺序应用。
func WithCleanupInterval ¶
WithCleanupInterval 设置后台清理间隔;0 关闭后台清理(默认 1 分钟)。
func WithDefaultTTL ¶
WithDefaultTTL 设置默认过期时长;0 表示永不过期(默认)。 显式 SetTTL 仍可覆盖单个条目。
func WithLRURefresh ¶
WithLRURefresh 设置 LRU 刷新策略:1 表示精确 LRU (每次命中都移动链表,默认);>1 表示采样刷新 (每 interval 次命中移动一次,写锁竞争降至 1/interval)。
func WithOnEvicted ¶
func WithOnEvicted(fn func(key string, value any, reason EvictReason)) Option
WithOnEvicted 设置逐出回调:容量逐出与过期清理时在锁外调用, 回调内可安全反查或写入 cache。Clear 不触发回调。
type Stats ¶
type Stats struct {
// Hits 命中次数。
Hits uint64
// Misses 未命中次数。
Misses uint64
// Sets 写入次数(含更新)。
Sets uint64
// Deletes 显式删除次数。
Deletes uint64
// Evictions 容量逐出次数。
Evictions uint64
// Expired 过期清理次数(惰性 + 后台)。
Expired uint64
// Len 当前条目数。
Len int
// Capacity 条目上限(0 表示不限)。
Capacity int
}
Stats 是缓存运行统计快照,并发安全。
Click to show internal directories.
Click to hide internal directories.