cache

package
v0.0.0-...-3cf0c3c Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 9 Imported by: 0

README

  1. 缓存雪崩 大量缓存在同一时间失效 解决方法:设置缓存时间为范围随机,解决大量缓存在同一时间失效的问题。
  2. 缓存穿透 缓存和数据库中都不存在(请求数据没有被缓存拦截,一直都在查询数据库,但是数据库没有,所以会一直找)

解决方法:当第一次命中时,如果没有数据会先查询数据,数据库没有查询到数据后设置该缓存 value 为 NOT_FOUND,之后每次都只会打到该缓存上,执行添加数据时把key进行删除 3. 缓存击穿

缓存击穿是指在缓存失效的时刻,大量的请求过来,回被同时打到下游,给下游造成瞬时压力,解决的办法就是只允许少量的请求打到下游,然后回填到缓存,其他请求还是从缓存拿数据。

解决办法一,使用分布式锁,确保只有一个请求获取到这个锁,其他请求去轮训缓存。特点是,为了保护下游,伤害了自己和redis,因为大量的请求停留在这里造成内存和CPU升高,但是大家都知道,接口服务器的性能不是问题,所以这个方案可行。

此处主要介绍方案二,那就是单飞模式 SingleFlight,在 golang 中有对应的实现 golang/groupcache ,它跟方案一的区别是讲分布式的锁改成了内存锁,没有了轮训redis这个操作;其次就是打到下游的请求要多一点,但是不构成压力。

解决方法:使用互斥锁,有锁时,等待获取。或可以使用SingleFlight请求合并/分布式锁

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDefaultNotFound = errors.New("cache: not found")
)

Functions

func FromSkipCacheContext

func FromSkipCacheContext(ctx context.Context) (skip bool)

FromSkipCacheContext 从上下文中获取跳过缓存变量

func NewCacheContext

func NewCacheContext(ctx context.Context, cache Cacher) context.Context

NewCacheContext ...

func NewSkipCacheContext

func NewSkipCacheContext(ctx context.Context, skip ...bool) context.Context

NewSkipCacheContext 创建跳过缓存到上下文

func RandomTimeHour

func RandomTimeHour(min, max int64) time.Duration

RandomTimeHour 随机多少小时 防止缓存雪崩:设置缓存时间为范围随机,解决大量缓存在同一时间失效的问题。

func RandomTimeMinute

func RandomTimeMinute(min, max int64) time.Duration

RandomTimeMinute 随机多少分钟 防止缓存雪崩:设置缓存时间为范围随机,解决大量缓存在同一时间失效的问题。

func RandomTimeSecond

func RandomTimeSecond(min, max int64) time.Duration

RandomTimeSecond 随机多少秒 防止缓存雪崩:设置缓存时间为范围随机,解决大量缓存在同一时间失效的问题。

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string, v interface{}) (err error)
	Task(ctx context.Context, key string, v interface{}, query QueryFunc, expiry ...time.Duration) (err error)
	Set(ctx context.Context, key string, v interface{}, expiry ...time.Duration) (err error)
	Del(ctx context.Context, keys ...string) (err error)
	AddRangeKey(ctx context.Context, setKey string, keys ...string) (err error)
	DelRangeKey(ctx context.Context, setKey string) (err error)
	IsNotFound(err error) bool
}

type CacheKey

type CacheKey struct{}

type Cacher

type Cacher interface {
	Geter
	Seter
	Remover
	Keyer
}

Cacher 缓存

func FromCacheContext

func FromCacheContext(ctx context.Context) (cache Cacher, ok bool)

FromCacheContext ...

type ExpiryFunc

type ExpiryFunc func() time.Duration

type Geter

type Geter interface {
	Get(ctx context.Context, key string, v interface{}) (err error)
	GetString(ctx context.Context, key string) (value string, err error)
	GetBytes(ctx context.Context, key string) (bytes []byte, err error)
	GetHash(ctx context.Context, key, field string) (value string)
}

Geter ...

type JSONSerialize

type JSONSerialize struct {
}

JSONSerialize 高性能json序列化

func (JSONSerialize) ContentType

func (JSONSerialize) ContentType() string

ContentType ...

func (JSONSerialize) Marshal

func (JSONSerialize) Marshal(msg interface{}) (data []byte, err error)

Marshal ...

func (JSONSerialize) Unmarshal

func (JSONSerialize) Unmarshal(data []byte, msg interface{}) (err error)

Unmarshal ...

type Keyer

type Keyer interface {
	// Zrem
	PushRangeKey(ctx context.Context, setKey string, keys ...string) (err error)
	DelRangeKey(ctx context.Context, setKey string) (err error)
}

type Option

type Option func(o *Options)

func WithErrNotFound

func WithErrNotFound(err error) Option

func WithExpiry

func WithExpiry(expiry ExpiryFunc) Option

func WithNotFoundExpiry

func WithNotFoundExpiry(expiry time.Duration) Option

func WithPrefix

func WithPrefix(prefix string) Option

func WithSerializer

func WithSerializer(serializer Serializer) Option

type Options

type Options struct {
	Expiry         ExpiryFunc
	NotFoundExpiry time.Duration
	Prefix         string
	Serializer     Serializer
	ErrNotFound    error
}

type QueryFunc

type QueryFunc func(v interface{}) error

type RedisCache

type RedisCache struct {
	Prefix string
	// contains filtered or unexported fields
}

RedisCache redis缓存

func NewRedisCache

func NewRedisCache(redisClient *redis.Client, prefix string) *RedisCache

NewRedisCache ...

func (*RedisCache) DelRangeKey

func (r *RedisCache) DelRangeKey(ctx context.Context, setKey string) (err error)

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string, v interface{}) (err error)

Get ...

func (*RedisCache) GetBytes

func (r *RedisCache) GetBytes(ctx context.Context, key string) (bytes []byte, err error)

GetBytes ...

func (*RedisCache) GetHash

func (r *RedisCache) GetHash(ctx context.Context, key, field string) (value string)

GetHash ...

func (*RedisCache) GetString

func (r *RedisCache) GetString(ctx context.Context, key string) (value string, err error)

GetString ...

func (*RedisCache) PushRangeKey

func (r *RedisCache) PushRangeKey(ctx context.Context, setKey string, keys ...string) (err error)

func (*RedisCache) Remove

func (r *RedisCache) Remove(ctx context.Context, keys ...string) (err error)

Remove ...

func (*RedisCache) RemoveMatch

func (r *RedisCache) RemoveMatch(ctx context.Context, match string) (err error)

RemoveMatch ...

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, v interface{}, expiration ...time.Duration) (err error)

Set ...

func (*RedisCache) SetBytes

func (r *RedisCache) SetBytes(ctx context.Context, key string, bytes []byte, expiration ...time.Duration) (err error)

SetBytes ...

func (*RedisCache) SetHash

func (r *RedisCache) SetHash(ctx context.Context, key string, v map[string]string, expiration ...time.Duration) (err error)

SetHash ...

func (*RedisCache) SetString

func (r *RedisCache) SetString(ctx context.Context, key string, value string, expiration ...time.Duration) (err error)

SetString ...

type RedisCache2

type RedisCache2 struct {
	// contains filtered or unexported fields
}

RedisCache2 redis缓存

func NewRedisCache2

func NewRedisCache2(redisClient *redis.Client, opts ...Option) *RedisCache2

NewRedisCache2 ...

func (*RedisCache2) AddRangeKey

func (r *RedisCache2) AddRangeKey(ctx context.Context, setKey string, keys ...string) (err error)

func (*RedisCache2) Del

func (r *RedisCache2) Del(ctx context.Context, keys ...string) (err error)

func (*RedisCache2) DelRangeKey

func (r *RedisCache2) DelRangeKey(ctx context.Context, setKey string) (err error)

func (*RedisCache2) Get

func (r *RedisCache2) Get(ctx context.Context, key string, v interface{}) (err error)

func (*RedisCache2) IsNotFound

func (r *RedisCache2) IsNotFound(err error) bool

func (*RedisCache2) Set

func (r *RedisCache2) Set(ctx context.Context, key string, v interface{}, expiry ...time.Duration) (err error)

func (*RedisCache2) Task

func (r *RedisCache2) Task(ctx context.Context, key string, v interface{}, query QueryFunc, expiry ...time.Duration) (err error)

type Remover

type Remover interface {
	Remove(ctx context.Context, keys ...string) (err error)
	RemoveMatch(ctx context.Context, match string) (err error)
}

Remover ...

type Serializer

type Serializer interface {
	Unmarshal(data []byte, msg interface{}) (err error)
	Marshal(msg interface{}) (data []byte, err error)
	ContentType() string
}

Serializer 序列化器

type Seter

type Seter interface {
	Set(ctx context.Context, key string, v interface{}, expiration ...time.Duration) (err error)
	SetString(ctx context.Context, key string, value string, expiration ...time.Duration) (err error)
	SetBytes(ctx context.Context, key string, bytes []byte, expiration ...time.Duration) (err error)
	SetHash(ctx context.Context, key string, v map[string]string, expiration ...time.Duration) (err error)
}

Seter ...

type SkipCacheKey

type SkipCacheKey struct{}

Jump to

Keyboard shortcuts

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