Documentation
¶
Index ¶
- Variables
- func Add(name string, fn Factory)
- func Clear()
- func Delete(ctx context.Context, key string) error
- func Get(ctx context.Context, key string, opts ...GetOption) (interface{}, error)
- func Len() int
- func Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error
- func SetDefault(d Cache)
- type Backend
- type Cache
- type Config
- type CreatorFunc
- type EvictCallback
- type Factory
- type Filter
- type GetOption
- type GetOptions
- type KeyModifier
- type LoadFunc
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotFactory = errors.New("cache: not find factory") ErrInvalidConfig = errors.New("cache: invalid config") ErrNotFound = errors.New("cache: not found") )
some error
Functions ¶
func SetDefault ¶
func SetDefault(d Cache)
Types ¶
type Backend ¶
type Backend interface {
Name() string
// Len 当前缓存中key的个数,若key已经过期但并没有及时清理,也会计算在内
Len() int
// Exists is used to check if the cache contains a key without updating recency or frequency.
Exists(ctx context.Context, key string) bool
Get(ctx context.Context, key string) (interface{}, error)
// Put ttl为0则不过期
Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Clear()
}
Backend cache 后端实现接口
type Cache ¶
type Cache interface {
// Len 当前缓存中key的个数,若key已经过期但并没有及时清理,也会计算在内
Len() int
// Exists is used to check if the cache contains a key without updating recency or frequency.
Exists(ctx context.Context, key string) bool
Get(ctx context.Context, key string, opts ...GetOption) (interface{}, error)
Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error
Delete(ctx context.Context, key string) error
// Clear 清空所有cache,仅内存cache支持
Clear()
}
Cache 缓存接口,可以是进程内cache,也可以是基于redis的分布式cache see: Guava,Caffeine 缓存常见问题:穿透,击穿,雪崩 https://zhuanlan.zhihu.com/p/75588064
其他库 https://github.com/dgraph-io/ristretto
TODO: support metrics, statistics, hook
type Config ¶
type Config struct {
Modifier KeyModifier // 修正key,添加前缀或后缀,默认不修改key
Filter Filter // 预先忽略不存在的key,防止击穿,可使用BloomFilter
AbsentTTL time.Duration // 用于标识是否缓存不存在的key,若为0则不缓存无效的key,默认为0,避免击穿
JitterTTL time.Duration // 当写入时,如果带有ttl,则会自动增加一个抖动时间,避免雪崩
MaxSize int // 限制最大条数
Codec encoding.Codec // 编解码,默认使用json
Creator CreatorFunc // 创建entity回调
OnEvict EvictCallback // 驱逐时回调
Extra map[string]interface{} // 扩展配置
}
Config cache 配置信息
type CreatorFunc ¶
type CreatorFunc func() interface{}
CreatorFunc 创建Entity,用于Get时,调用Unmarshal entity
type EvictCallback ¶
type EvictCallback func(key interface{}, value interface{})
EvictCallback is used to get a callback when a cache entry is evicted
type Filter ¶
type Filter interface {
// Exists 判断key是否存在,不存在则返回false,直接忽略
Exists(ctx context.Context, key string) bool
}
Filter 用于过滤不存在的key,防止击穿,通常使用BloomFilter实现
type GetOption ¶
type GetOption func(o *GetOptions)
type GetOptions ¶
type GetOptions struct {
Load LoadFunc
}
type KeyModifier ¶
KeyModifier 修正key,添加前缀或者后缀
Click to show internal directories.
Click to hide internal directories.