cache

package
v0.0.0-...-817968b Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

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 Add

func Add(name string, fn Factory)

Add 注册factory

func Clear

func Clear()

Clear 默认Cache Clear

func Delete

func Delete(ctx context.Context, key string) error

Delete 默认Cache Delete

func Get

func Get(ctx context.Context, key string, opts ...GetOption) (interface{}, error)

Get 使用默认Cache Get

func Len

func Len() int

Len 默认Cache Len

func Put

func Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Put 默认Cache Put

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

func MustNew

func MustNew(name string, conf *Config) Cache

MustNew 创建cache,异常会抛panic

func New

func New(name string, conf *Config) (Cache, error)

New 新建cache

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 配置信息

func (*Config) Get

func (c *Config) Get(key string) interface{}

Get 通过key获取extra配置

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 Factory

type Factory func(conf *Config) (Backend, error)

Factory .

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)

func WithLoad

func WithLoad(fn LoadFunc) GetOption

WithLoad .

type GetOptions

type GetOptions struct {
	Load LoadFunc
}

type KeyModifier

type KeyModifier func(key string) string

KeyModifier 修正key,添加前缀或者后缀

func Prefix

func Prefix(prefix string) KeyModifier

Prefix key添加前缀

func Suffix

func Suffix(suffix string) KeyModifier

Suffix key添加后缀

type LoadFunc

type LoadFunc func(ctx context.Context, key string) ([]byte, error)

GetFunc 当缓存不存在时,加载回调函数,可使用singleflight方式避免缓存击穿

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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