caching

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 实例

func (*CacheAside) Delete

func (ca *CacheAside) Delete(ctx context.Context, key string, deleter func() error) error

Delete 同时从缓存和源删除

func (*CacheAside) Get

func (ca *CacheAside) Get(ctx context.Context, key string, loader func() (string, error)) (string, error)

Get 使用 cache-aside 模式读取数据

func (*CacheAside) Set

func (ca *CacheAside) Set(ctx context.Context, key string, value string, writer func() error) error

Set 同时更新缓存和源

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

func NewRefreshAhead(c KVCache, ttl time.Duration, refreshBefore time.Duration) *RefreshAhead

NewRefreshAhead 创建 RefreshAhead 实例

func (*RefreshAhead) Get

func (ra *RefreshAhead) Get(ctx context.Context, key string, loader func() (string, error)) (string, error)

Get retrieves data with automatic refresh scheduling

func (*RefreshAhead) Stop

func (ra *RefreshAhead) Stop()

Stop 停止后台刷新器

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) Set

func (wb *WriteBehind) Set(ctx context.Context, key string, value string) error

Set updates cache immediately and queues DB write

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

func (*WriteBehind) Stop

func (wb *WriteBehind) Stop()

Stop stops the background processor

type WriteBehindOption

type WriteBehindOption func(*WriteBehind)

func WithWriteBehindWriter

func WithWriteBehindWriter(writer WriteBehindWriter) WriteBehindOption

type WriteBehindWriter

type WriteBehindWriter func(ctx context.Context, key string, value string) error

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 实例

func (*WriteThrough) Get

func (wt *WriteThrough) Get(ctx context.Context, key string, loader func() (string, error)) (string, error)

Get 使用 write-through 模式读取数据

func (*WriteThrough) Set

func (wt *WriteThrough) Set(ctx context.Context, key string, value string, writer func() error) error

Set 原子地写入缓存和源

Jump to

Keyboard shortcuts

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