cache

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheClosed      = errors.New("缓存已经被关闭")
	ErrCacheKeyNotExist = errors.New("key不存在")
	ErrCacheFull        = errors.New("缓存满了")
)

Functions

This section is empty.

Types

type BloomFilter

type BloomFilter func(ctx context.Context, key string) bool

type BloomFilterCache

type BloomFilterCache struct {
	ReadThroughCache
	// contains filtered or unexported fields
}

BloomFilterCache 布隆过滤器cache,用于解决缓存穿透的问题,当攻击者伪造大量不同key攻击时会直接打到数据库, 这个cache再查库之前先调用用户传进来的Exist方法看有没有这个key

func (*BloomFilterCache) Get

func (c *BloomFilterCache) Get(ctx context.Context, key string) (any, error)

type BulidinMapCache

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

func NewBuildinMapCache

func NewBuildinMapCache(opts ...CacheOption) *BulidinMapCache

func (*BulidinMapCache) Close

func (c *BulidinMapCache) Close() error

func (*BulidinMapCache) Delete

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

func (*BulidinMapCache) Expire

func (c *BulidinMapCache) Expire(key string, expiration time.Duration) error

func (*BulidinMapCache) Get

func (c *BulidinMapCache) Get(ctx context.Context, key string) (any, error)

func (*BulidinMapCache) OnEvicted

func (c *BulidinMapCache) OnEvicted(fn func(key string, val any))

func (*BulidinMapCache) Set

func (c *BulidinMapCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

func (*BulidinMapCache) TTL

func (c *BulidinMapCache) TTL(ctx context.Context, key string) (time.Duration, error)

type Cache

type Cache interface {
	Get(ctx context.Context, key string) (any, error)
	Set(ctx context.Context, key string, val any, expiration time.Duration) error
	Delete(ctx context.Context, key string) error
}

Cache 值的类型问题: string:可以,问题是本地缓存,结构体转化为string,用json序列化 []byte最通用的表达,可以存储序列化后的数据,也可以存加密数据,但是用起来不方便 any:方便,redis之类的实现,需要考虑序列化问题 提高内存使用率:不要直接存对象,序列化(紧凑的算法如protobuf)+压缩 不使用map,用树形结构等 为什么不定义泛型?定义泛型用户只能在cache里面存一种数据了

type CacheOption

type CacheOption func(b *BulidinMapCache)

func WithCycleInterval

func WithCycleInterval(interval time.Duration) CacheOption

func WithOnEvicted

func WithOnEvicted(onEvicted func(key string, val any)) CacheOption

type LoadFunc

type LoadFunc func(ctx context.Context, key string) (any, error)

type LocalCache

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

LocalCache 过期key删除采用懒惰关闭+轮询关闭

func NewLocalCache

func NewLocalCache(onEvicted func(key string, val any)) *LocalCache

func (*LocalCache) Close

func (l *LocalCache) Close() error

Close 需要考虑用户重复close

func (*LocalCache) Delete

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

func (*LocalCache) Get

func (l *LocalCache) Get(ctx context.Context, key string) (any, error)

func (*LocalCache) Set

func (l *LocalCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type LocalCacheDelayQueue

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

LocalCacheDelayQueue 轮询过期和延时队列二选一,如下是延时队列实现

func NewLocalCacheDelayQueue

func NewLocalCacheDelayQueue(size int) *LocalCacheDelayQueue

func (*LocalCacheDelayQueue) AutoExpire

func (c *LocalCacheDelayQueue) AutoExpire()

AutoExpire 用延时队列处理过期key

func (*LocalCacheDelayQueue) Close

func (c *LocalCacheDelayQueue) Close() error

func (*LocalCacheDelayQueue) Delete

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

func (*LocalCacheDelayQueue) Get

func (c *LocalCacheDelayQueue) Get(ctx context.Context, key string) (any, error)

func (*LocalCacheDelayQueue) Set

func (c *LocalCacheDelayQueue) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type MaxCntCache

type MaxCntCache struct {
	Cache

	MaxCount int32
	// contains filtered or unexported fields
}

func NewMaxCntCache

func NewMaxCntCache(maxcount int32) *MaxCntCache

func (*MaxCntCache) Set

func (m *MaxCntCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type PreloadCache

type PreloadCache struct {
	SentinelCache Cache
	ReadThroughCache
	// contains filtered or unexported fields
}

PreloadCache 预加载cache,set的时候同时set哨兵cache和实际cache,哨兵cache只存储key, 且过期时间要比主cache早,哨兵cache过期的时候执行回调: 执行主cache的loadfunc刷新主cache的缓存。 适合对读性能(缓存命中率)要求较高的场景

func NewPreloadCache

func NewPreloadCache(expiration time.Duration, onEvicted func(key string, val any), loadFunc LoadFunc) *PreloadCache

func (*PreloadCache) Set

func (c *PreloadCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type RandomExpirationCache

type RandomExpirationCache struct {
	Cache
	// contains filtered or unexported fields
}

RandomExpirationCache 增加随机偏移过期时间,以防key统一过期造成缓存雪崩

func NewRandomExpirationCache

func NewRandomExpirationCache(cache Cache, offset func() time.Duration) *RandomExpirationCache

func (RandomExpirationCache) Set

func (c RandomExpirationCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type ReadThroughCache

type ReadThroughCache struct {
	Cache
	Expiration time.Duration
	//把捞DB抽象为“加载数据”
	LoadFunc
}

func NewReadThroughCache

func NewReadThroughCache(cache Cache, Expiration time.Duration, loadFunc LoadFunc) *ReadThroughCache

func (*ReadThroughCache) Get

func (c *ReadThroughCache) Get(ctx context.Context, key string) (any, error)

Get 加锁问题:先穿透读 再有人写数据库,数据就会不一致;加了写锁也会有不一致,保证了读,但直接写cache就会不一致

type RedisCache

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

func NewRedisCache

func NewRedisCache(client redis.Cmdable) *RedisCache

NewRedisCache 面向接口编程,依赖注入,不要传一个string的地址自己建redisClient,要不然单元测试就会尝试连这个addr,没办法测,我们需要mockredis

func (*RedisCache) Delete

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

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) (any, error)

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

type SingleFlightCache

type SingleFlightCache struct {
	ReadThroughCache
	// contains filtered or unexported fields
}

SingleFlightCache 采用singleFlight调用去访问数据库,解决缓存击穿的问题 保证相同的key只有一个goroutine会实际查询数据库,调用完成forget一下让其它等待的goroutine不等了

func (*SingleFlightCache) Get

func (c *SingleFlightCache) Get(ctx context.Context, key string) (any, error)

type StoreFunc

type StoreFunc func(ctx context.Context, key string, val any) error

type WriteBackCache

type WriteBackCache struct {
	*LocalCache
	StoreFunc
}

WriteBackCache 采用localcache的删除回调机制实现缓存过期刷新到db

func NewWriteBackCache

func NewWriteBackCache(storeFunc StoreFunc) *WriteBackCache

func (*WriteBackCache) Close

func (c *WriteBackCache) Close() error

type WriteThroughCache

type WriteThroughCache struct {
	Cache
	Expiration time.Duration
	//把捞DB抽象为“加载数据”
	StoreFunc
}

func NewWriteThroughCache

func NewWriteThroughCache(cache Cache, Expiration time.Duration, storeFunc StoreFunc) *WriteThroughCache

func (*WriteThroughCache) Set

func (c *WriteThroughCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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