Documentation ¶
Index ¶
- Variables
- type BloomFilter
- type BloomFilterCache
- type BulidinMapCache
- func (c *BulidinMapCache) Close() error
- func (c *BulidinMapCache) Delete(ctx context.Context, key string) error
- func (c *BulidinMapCache) Expire(key string, expiration time.Duration) error
- func (c *BulidinMapCache) Get(ctx context.Context, key string) (any, error)
- func (c *BulidinMapCache) OnEvicted(fn func(key string, val any))
- func (c *BulidinMapCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error
- func (c *BulidinMapCache) TTL(ctx context.Context, key string) (time.Duration, error)
- type Cache
- type CacheOption
- type LoadFunc
- type LocalCache
- type LocalCacheDelayQueue
- func (c *LocalCacheDelayQueue) AutoExpire()
- func (c *LocalCacheDelayQueue) Close() error
- func (c *LocalCacheDelayQueue) Delete(ctx context.Context, key string) error
- func (c *LocalCacheDelayQueue) Get(ctx context.Context, key string) (any, error)
- func (c *LocalCacheDelayQueue) Set(ctx context.Context, key string, val any, expiration time.Duration) error
- type MaxCntCache
- type PreloadCache
- type RandomExpirationCache
- type ReadThroughCache
- type RedisCache
- type SingleFlightCache
- type StoreFunc
- type WriteBackCache
- type WriteThroughCache
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type BloomFilterCache ¶
type BloomFilterCache struct { ReadThroughCache // contains filtered or unexported fields }
BloomFilterCache 布隆过滤器cache,用于解决缓存穿透的问题,当攻击者伪造大量不同key攻击时会直接打到数据库, 这个cache再查库之前先调用用户传进来的Exist方法看有没有这个key
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) OnEvicted ¶
func (c *BulidinMapCache) OnEvicted(fn func(key string, val any))
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 LocalCache ¶
type LocalCache struct {
// contains filtered or unexported fields
}
LocalCache 过期key删除采用懒惰关闭+轮询关闭
func NewLocalCache ¶
func NewLocalCache(onEvicted func(key string, val any)) *LocalCache
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
type MaxCntCache ¶
func NewMaxCntCache ¶
func NewMaxCntCache(maxcount int32) *MaxCntCache
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 ¶
type RandomExpirationCache ¶
type RandomExpirationCache struct { Cache // contains filtered or unexported fields }
RandomExpirationCache 增加随机偏移过期时间,以防key统一过期造成缓存雪崩
func NewRandomExpirationCache ¶
func NewRandomExpirationCache(cache Cache, offset func() time.Duration) *RandomExpirationCache
type ReadThroughCache ¶
func NewReadThroughCache ¶
func NewReadThroughCache(cache Cache, Expiration time.Duration, loadFunc LoadFunc) *ReadThroughCache
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
func NewRedisCache ¶
func NewRedisCache(client redis.Cmdable) *RedisCache
NewRedisCache 面向接口编程,依赖注入,不要传一个string的地址自己建redisClient,要不然单元测试就会尝试连这个addr,没办法测,我们需要mockredis
type SingleFlightCache ¶
type SingleFlightCache struct { ReadThroughCache // contains filtered or unexported fields }
SingleFlightCache 采用singleFlight调用去访问数据库,解决缓存击穿的问题 保证相同的key只有一个goroutine会实际查询数据库,调用完成forget一下让其它等待的goroutine不等了
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 ¶
func NewWriteThroughCache ¶
func NewWriteThroughCache(cache Cache, Expiration time.Duration, storeFunc StoreFunc) *WriteThroughCache