Documentation
¶
Index ¶
- Variables
- type CacheAside
- type KVCache
- type RefreshAhead
- type WriteBehind
- func (wb *WriteBehind) Flush(timeout time.Duration)
- func (wb *WriteBehind) FlushContext(ctx context.Context) error
- func (wb *WriteBehind) Get(ctx context.Context, key string, loader func() (string, error)) (string, error)
- func (wb *WriteBehind) Set(ctx context.Context, key string, value string) error
- func (wb *WriteBehind) SetWithWriter(ctx context.Context, key string, value string, writer func() error) error
- func (wb *WriteBehind) Stop()
- type WriteBehindOption
- type WriteBehindWriter
- type WriteThrough
Constants ¶
This section is empty.
Variables ¶
var ErrCacheMiss = errors.New("cache miss")
ErrCacheMiss 表示缓存中不存在该 key;后端故障应返回其他错误。
Functions ¶
This section is empty.
Types ¶
type CacheAside ¶
type CacheAside struct {
// contains filtered or unexported fields
}
CacheAside 实现 Cache-Aside(旁路缓存)模式。 读:查缓存 -> 未命中则从源加载 -> 回写缓存 -> 返回 写:更新源 -> 失效缓存
func NewCacheAside ¶
func NewCacheAside(c KVCache, ttl time.Duration) *CacheAside
NewCacheAside 创建 CacheAside 实例
type KVCache ¶
type KVCache interface {
Get(ctx context.Context, key string) (string, error)
Set(ctx context.Context, key string, value string, ttl time.Duration) error
Del(ctx context.Context, key string) error
}
KVCache 定义缓存模式所需的最小 KV 操作接口。 这是一个本地接口,不依赖 infra/cache —— 任何具备 Get/Set/Del 能力的 缓存客户端(如 RedisCache)都隐式满足此接口,可直接传入。
type RefreshAhead ¶
type RefreshAhead struct {
// contains filtered or unexported fields
}
RefreshAhead 实现 Refresh-Ahead(提前刷新)模式。 在缓存过期前主动刷新,避免读请求命中过期缓存。
func NewRefreshAhead ¶
NewRefreshAhead 创建 RefreshAhead 实例
type WriteBehind ¶
type WriteBehind struct {
// contains filtered or unexported fields
}
WriteBehind 实现 Write-Behind(异步写回)模式。 写:立即更新缓存 -> 异步写入源 读:查缓存 -> 未命中则从源读取 -> 回写缓存
func NewWriteBehind ¶
func NewWriteBehind(c KVCache, ttl time.Duration, queueSize int, opts ...WriteBehindOption) *WriteBehind
NewWriteBehind 创建 WriteBehind 实例
func (*WriteBehind) Flush ¶
func (wb *WriteBehind) Flush(timeout time.Duration)
Flush flushes remaining items in queue
func (*WriteBehind) FlushContext ¶
func (wb *WriteBehind) FlushContext(ctx context.Context) error
func (*WriteBehind) Get ¶
func (wb *WriteBehind) Get(ctx context.Context, key string, loader func() (string, error)) (string, error)
Get retrieves data using write-behind pattern
func (*WriteBehind) SetWithWriter ¶
func (wb *WriteBehind) SetWithWriter(ctx context.Context, key string, value string, writer func() error) error
SetWithWriter updates cache and queues custom writer
type WriteBehindOption ¶
type WriteBehindOption func(*WriteBehind)
func WithWriteBehindWriter ¶
func WithWriteBehindWriter(writer WriteBehindWriter) WriteBehindOption
type WriteBehindWriter ¶
type WriteThrough ¶
type WriteThrough struct {
// contains filtered or unexported fields
}
WriteThrough 实现 Write-Through(写穿透)模式。 写:更新缓存和源在同一事务中完成 读:查缓存 -> 未命中则从源读取 -> 回写缓存
func NewWriteThrough ¶
func NewWriteThrough(c KVCache, ttl time.Duration) *WriteThrough
NewWriteThrough 创建 WriteThrough 实例