Documentation
¶
Index ¶
- Variables
- func Del(ctx context.Context, key string) error
- func Get(ctx context.Context, key string) (string, error)
- func GetService(ctx context.Context) (datacontract.Cache, error)
- func MGet(ctx context.Context, keys ...string) (map[string]string, error)
- func MustGetService(ctx context.Context) datacontract.Cache
- func Remember(ctx context.Context, key string, ttl time.Duration, ...) (string, error)
- func Set(ctx context.Context, key, value string, ttl time.Duration) error
- type Cache
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrCacheMiss = datacontract.ErrCacheMiss
ErrCacheMiss indicates that the cache key does not exist. ErrCacheMiss 表示缓存未命中。
Functions ¶
func GetService ¶
func GetService(ctx context.Context) (datacontract.Cache, error)
GetService 从容器获取统一缓存服务。 通过 ctx 解析框架容器后再解析缓存实例。
func MustGetService ¶ added in v0.1.4
func MustGetService(ctx context.Context) datacontract.Cache
MustGetService 从容器获取统一缓存服务,失败时 panic。 用于业务初始化阶段,确认缓存服务必须可用的场景。
func Remember ¶
func Remember(ctx context.Context, key string, ttl time.Duration, fn func(context.Context) (string, error)) (string, error)
Remember 读取缓存,未命中时回源计算并回填。 先尝试读取 key 的缓存值;若缓存未命中,则调用 fn 计算结果, 将结果写入缓存(ttl 过期时间)并返回。
Example:
value, err := cache.Remember(ctx, "user:42", time.Minute, func(ctx context.Context) (string, error) {
return loadUserJSON(ctx, 42)
})
Types ¶
type Cache ¶
type Cache = datacontract.Cache
Cache is the top-level alias of the unified cache contract. Cache 是统一缓存契约的顶层别名。
Click to show internal directories.
Click to hide internal directories.