Documentation
¶
Overview ¶
Package cache 提供类似 Caffeine 的本地缓存实现,采用 LRU 淘汰策略。
CaffeineCache 是一个实现 Cache 接口的高性能本地缓存。 当缓存达到最大容量时,它提供 LRU(最近最少使用)淘汰策略, 并为单个缓存条目提供 TTL(生存时间)支持。
用法:
cache := cache.NewCaffeineCache( cache.WithCaffeineMaxSize(1000), cache.WithCaffeineDefaultTTL(5*time.Minute), ) cache.Set(ctx, "key", "value", 10*time.Minute) value, err := cache.Get(ctx, "key")
Package cache 提供缓存抽象层,用于 enhance 框架。
该模块提供多种缓存策略实现,包括 LRU 缓存和 Caffeine 缓存。 参考 Spring Cache 的设计理念,提供统一的缓存抽象接口。
架构设计 ¶
- Cache: 缓存操作接口
- Getter: 缓存获取器,支持缓存穿透保护
- Builder: 缓存构建器,支持链式配置
- LRUCache: LRU(Least Recently Used)缓存实现
- ShardedLRUCache: 分片 LRU 缓存,适合高并发场景
- CaffeineCache: 类似 Caffeine 的高性能缓存实现
支持的缓存策略 ¶
- LRU 缓存: 基于最近最少使用算法的缓存实现
- Caffeine 缓存: 类似 Java Caffeine 的高性能缓存实现
- 分片缓存: 支持并发安全的分片 LRU 缓存
使用方式 ¶
使用 LRU 缓存:
cache := cache.NewLRUCache(1000) // 最大 1000 个条目 cache.Set(context.Background(), "key", "value", 5*time.Minute) value, err := cache.Get(context.Background(), "key")
使用缓存构建器:
cache := cache.NewMemoryCacheBuilder().
InitialCapacity(1000).
TTL(5*time.Minute).
Build()
使用缓存获取器(带穿透保护):
getter := cache.NewGetter(func(key string) (any, error) {
// 从数据库加载
return loadFromDB(key)
})
value, err := getter.Get("key")
Package cache 提供缓存抽象层,用于 enhance 框架。
Package cache 提供缓存抽象层,用于 enhance 框架。
Index ¶
- Variables
- func NewCaffeineCache(opts ...CaffeineOption) *caffeineCache
- type Cache
- type CacheConfig
- type CacheHelper
- func (h *CacheHelper) Clear(ctx context.Context) error
- func (h *CacheHelper) Exists(ctx context.Context, key string) (bool, error)
- func (h *CacheHelper) Get(ctx context.Context, key string) (any, error)
- func (h *CacheHelper) GetOrSet(ctx context.Context, key string, fn func() (any, error), ttl time.Duration) (any, error)
- func (h *CacheHelper) Invalidate(ctx context.Context, key string) error
- func (h *CacheHelper) InvalidateAll(ctx context.Context, keys ...string) error
- func (h *CacheHelper) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (h *CacheHelper) TTL(ctx context.Context, key string) (time.Duration, error)
- type CacheOption
- type CacheTemplate
- func (t *CacheTemplate) Del(ctx context.Context, key string) error
- func (t *CacheTemplate) Exists(ctx context.Context, key string) (bool, error)
- func (t *CacheTemplate) Get(ctx context.Context, key string) (any, error)
- func (t *CacheTemplate) GetOrSet(ctx context.Context, key string, fn func() (any, error), ttl time.Duration) (any, error)
- func (t *CacheTemplate) Key(key string) string
- func (t *CacheTemplate) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (t *CacheTemplate) TTL(ctx context.Context, key string) (time.Duration, error)
- type CaffeineOption
- type Getter
- type LRUCache
- func (c *LRUCache) Clear()
- func (c *LRUCache) Close() error
- func (c *LRUCache) Del(ctx context.Context, keys ...string) error
- func (c *LRUCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *LRUCache) Get(ctx context.Context, key string) (any, error)
- func (c *LRUCache) Len() int
- func (c *LRUCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *LRUCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type LRUOption
- type MemoryCacheBuilder
- type ShardedLRUCache
- func (c *ShardedLRUCache) Clear()
- func (c *ShardedLRUCache) Close() error
- func (c *ShardedLRUCache) Del(ctx context.Context, keys ...string) error
- func (c *ShardedLRUCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *ShardedLRUCache) Get(ctx context.Context, key string) (any, error)
- func (c *ShardedLRUCache) Len() int
- func (c *ShardedLRUCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *ShardedLRUCache) TTL(ctx context.Context, key string) (time.Duration, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound 缓存键不存在。 ErrNotFound = errors.New("cache: key not found") // ErrCacheMiss 缓存键已过期或不存在。 ErrCacheMiss = errors.New("cache: key expired or not found") )
Functions ¶
func NewCaffeineCache ¶
func NewCaffeineCache(opts ...CaffeineOption) *caffeineCache
NewCaffeineCache 创建一个新的类 Caffeine 本地缓存,采用 LRU 淘汰策略。
该缓存使用 map 实现 O(1) 查找,使用双向链表实现 O(1) LRU 跟踪。默认配置:
- 最大容量:1000 个条目
- 默认 TTL:5 分钟
Types ¶
type Cache ¶
type Cache interface {
// Get 获取指定键的缓存值,不存在返回 ErrNotFound。
Get(ctx context.Context, key string) (any, error)
// Set 设置缓存键值,ttl<=0 表示永不过期。
Set(ctx context.Context, key string, value any, ttl time.Duration) error
// Del 删除指定的缓存键。
Del(ctx context.Context, keys ...string) error
// Exists 检查键是否存在且未过期。
Exists(ctx context.Context, key string) (bool, error)
// TTL 获取键的剩余过期时间。
TTL(ctx context.Context, key string) (time.Duration, error)
// Close 关闭缓存连接并释放资源。
Close() error
}
Cache 缓存操作接口。
所有缓存实现都应该实现此接口, 以便与 enhance 的依赖注入系统集成。
type CacheConfig ¶
type CacheConfig struct {
Enabled bool // 是否启用缓存
DefaultTTL time.Duration // 默认TTL
MaxSize int // 最大缓存项数
KeyPrefix string // 键前缀
StatsEnabled bool // 是否启用统计
}
CacheConfig 缓存配置
func (*CacheConfig) ApplyOptions ¶
func (c *CacheConfig) ApplyOptions(opts []CacheOption)
ApplyOptions 应用配置选项
type CacheHelper ¶
type CacheHelper struct {
// contains filtered or unexported fields
}
CacheHelper 缓存辅助工具,简化常见缓存操作
func (*CacheHelper) GetOrSet ¶
func (h *CacheHelper) GetOrSet(ctx context.Context, key string, fn func() (any, error), ttl time.Duration) (any, error)
GetOrSet 获取缓存值,如果不存在则使用提供的函数获取并缓存
func (*CacheHelper) Invalidate ¶
func (h *CacheHelper) Invalidate(ctx context.Context, key string) error
Invalidate 使缓存失效
func (*CacheHelper) InvalidateAll ¶
func (h *CacheHelper) InvalidateAll(ctx context.Context, keys ...string) error
InvalidateAll 使所有指定键的缓存失效
type CacheTemplate ¶
type CacheTemplate struct {
// contains filtered or unexported fields
}
CacheTemplate 缓存模板,提供常用的缓存操作模板
func NewCacheTemplate ¶
func NewCacheTemplate(cache Cache, prefix string) *CacheTemplate
NewCacheTemplate 创建缓存模板
func (*CacheTemplate) Del ¶
func (t *CacheTemplate) Del(ctx context.Context, key string) error
Del 删除缓存键
func (*CacheTemplate) GetOrSet ¶
func (t *CacheTemplate) GetOrSet(ctx context.Context, key string, fn func() (any, error), ttl time.Duration) (any, error)
GetOrSet 获取或设置缓存值
type CaffeineOption ¶
type CaffeineOption func(*caffeineCache)
CaffeineOption configures the Caffeine cache.
func WithCaffeineDefaultTTL ¶
func WithCaffeineDefaultTTL(ttl time.Duration) CaffeineOption
WithCaffeineDefaultTTL 设置缓存条目的默认 TTL。 当调用 Set() 且 ttl <= 0 时使用此 TTL。
func WithCaffeineMaxSize ¶
func WithCaffeineMaxSize(maxSize int) CaffeineOption
WithCaffeineMaxSize 设置缓存中的最大条目数。 当缓存达到此大小时,最近最少使用的条目将被淘汰。
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache LRU(最近最少使用)缓存实现。
基于双向链表和哈希表实现 O(1) 时间复杂度的缓存操作。 支持 TTL 过期淘汰和容量限制淘汰。 LRUCache 是并发安全的,所有操作都通过互斥锁保护。
func NewLRUCache ¶
NewLRUCache 创建 LRU 缓存。
参数:
- capacity: 缓存容量,必须大于 0(<=0 时使用默认值 100)
- opts: 可选配置项
返回:
- *LRUCache: LRU 缓存实例
func (*LRUCache) Exists ¶
Exists 检查键是否存在且未过期。
参数:
- ctx: 上下文(保留用于接口一致性,当前未使用)
- key: 缓存键
返回值:
- bool: 键存在且未过期返回 true
- error: 始终返回 nil
func (*LRUCache) Get ¶
Get 获取缓存值。
如果键不存在或已过期,返回 ErrNotFound。 命中缓存时会将该键移到链表前端(标记为最近使用)。
参数:
- ctx: 上下文(保留用于接口一致性,当前未使用)
- key: 缓存键
返回值:
- any: 缓存值
- error: 键不存在或已过期时返回 ErrNotFound
type LRUOption ¶
type LRUOption func(*LRUCache)
LRUOption LRU 缓存选项函数。
type MemoryCacheBuilder ¶
type MemoryCacheBuilder struct {
// contains filtered or unexported fields
}
MemoryCacheBuilder 内存缓存构建器,支持链式配置
func NewMemoryCacheBuilder ¶
func NewMemoryCacheBuilder() *MemoryCacheBuilder
NewMemoryCacheBuilder 创建内存缓存构建器
func (*MemoryCacheBuilder) InitialCapacity ¶
func (b *MemoryCacheBuilder) InitialCapacity(capacity int) *MemoryCacheBuilder
InitialCapacity 设置初始容量
func (*MemoryCacheBuilder) MustBuild ¶
func (b *MemoryCacheBuilder) MustBuild() Cache
MustBuild 构建 LRU 缓存
func (*MemoryCacheBuilder) TTL ¶
func (b *MemoryCacheBuilder) TTL(ttl time.Duration) *MemoryCacheBuilder
TTL 设置默认过期时间
type ShardedLRUCache ¶
type ShardedLRUCache struct {
// contains filtered or unexported fields
}
ShardedLRUCache 分片 LRU 缓存实现。
将全局锁拆分为多个分段锁,提升高并发场景下的性能。 每个分片独立维护自己的 LRU 链表和哈希表。
func NewShardedLRUCache ¶
func NewShardedLRUCache(capacity int, shardCount int, opts ...LRUOption) *ShardedLRUCache
NewShardedLRUCache 创建分片 LRU 缓存
参数:
- capacity: 总容量,将平均分配到各分片
- shardCount: 分片数量,建议为 2 的幂次(如 16, 32, 64)
- opts: 可选配置项
返回:
- *ShardedLRUCache: 分片 LRU 缓存实例
func (*ShardedLRUCache) Del ¶
func (c *ShardedLRUCache) Del(ctx context.Context, keys ...string) error
Del 删除缓存项