bigcache
BigCache是如何做到高性能的?
加速并发访问
避免GC开销
BigCache的原理
type BigCache struct {
shards []*cacheShard // 缓存分片数据
lifeWindow uint64
clock clock
hash Hasher
config Config
shardMask uint64
close chan struct{}
}
type cacheShard struct {
hashmap map[uint64]uint32 // 索引,对应KEY在entries中的位置和长度
entries queue.BytesQueue // 实际数据存储
lock sync.RWMutex
entryBuffer []byte
onRemove onRemoveCallback
isVerbose bool
statsEnabled bool
logger Logger
clock clock
lifeWindow uint64
hashmapStats map[uint64]uint32
stats Stats
}
type BytesQueue struct {
full bool
array []byte // 实际数据存储
capacity int
maxCapacity int
head int
tail int // 下次可以插入的位置
count int
rightMargin int
headerBuffer []byte
verbose bool
}过期策略
一个例子
LifeWindow & CleanWindow
Last updated