cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 10 Imported by: 0

README

这里主要是关于缓存的一些库,主要基于内存型和NoSQL的

内存型的有:memory和big_cache NoSQL的主要有: redis

各类库只要实现了cache定义的接口(driver)即可。

这里的接口driver命名参考了Go官方mysql接口的命名规范

多级缓存

二级缓存

这里的多级主要是指二级缓存:本地缓存(一级缓存L1)+redis缓存(二级缓存L2)
使用本地缓存可以减少应用服务器到redis之间的网络I/O开销

需要注意的是:在并发量不大的系统内,本地缓存的意义不大,反而增加维护的困难。但在高并发系统中, 本地缓存可以大大节约带宽。但是要注意本地缓存不是银弹,它会引起多个副本间数据的 不一致,还会占据大量的内存,所以不适合保存特别大的数据,而且需要严格考虑刷新机制。

过期时间

本地缓存过期时间比分布式缓存小至少一半,以防止本地缓存太久造成多实例数据不一致。

缓存问题

需要注意以下几个问题

  • 缓存穿透
  • 缓存击穿
  • 缓存雪崩

可以参考文章:Redis缓存三大问题

Reference

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultExpireTime 默认过期时间
	DefaultExpireTime = time.Hour * 24
	// DefaultNotFoundExpireTime 结果为空时的过期时间 1分钟, 常用于数据为空时的缓存时间(缓存穿透)
	DefaultNotFoundExpireTime = time.Minute
	// NotFoundPlaceholder .
	NotFoundPlaceholder = "*"

	// DefaultClient 生成一个缓存客户端,其中keyPrefix 一般为业务前缀
	DefaultClient Cache

	// ErrPlaceholder .
	ErrPlaceholder = errors.New("cache: placeholder")
	// ErrSetMemoryWithNotFound .
	ErrSetMemoryWithNotFound = errors.New("cache: set memory cache err for not found")
)

Functions

func BuildCacheKey

func BuildCacheKey(keyPrefix string, key string) (cacheKey string, err error)

BuildCacheKey 构建一个带有前缀的缓存key

func Del

func Del(ctx context.Context, keys ...string) error

Del 批量删除

func Get

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

Get 数据

func MultiGet

func MultiGet(ctx context.Context, keys []string, valueMap interface{}) error

MultiGet 批量获取

func MultiSet

func MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error

MultiSet 批量set

func Set

func Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error

Set 数据

func SetCacheWithNotFound

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

SetCacheWithNotFound .

Types

type Cache

type Cache interface {
	Set(ctx context.Context, key string, val interface{}, expiration time.Duration) error
	Get(ctx context.Context, key string, val interface{}) error
	MultiSet(ctx context.Context, valMap map[string]interface{}, expiration time.Duration) error
	MultiGet(ctx context.Context, keys []string, valueMap interface{}) error
	Del(ctx context.Context, keys ...string) error
	SetCacheWithNotFound(ctx context.Context, key string) error
}

Cache 定义cache驱动接口

func NewMemoryCache

func NewMemoryCache(keyPrefix string, encoding encoding.Encoding) Cache

NewMemoryCache create a memory cache

func NewRedisCache

func NewRedisCache(client *redis.Client, keyPrefix string, encoding encoding.Encoding, newObject func() interface{}) Cache

NewRedisCache new一个cache, client 参数是可传入的,方便进行单元测试

type GetFunc

type GetFunc func(key string) interface{}

GetFunc .

func (GetFunc) Get

func (f GetFunc) Get(key string) interface{}

Get .

type Getter

type Getter interface {
	Get(key string) interface{}
}

Getter .

Jump to

Keyboard shortcuts

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