先讲单shard的LRU cache,主要有3个数据结构
LRUHandle 作为LRU的一个个体元素,其数据结构如下,next,prev是在LRU的双向列表的next和prev,next_hash是在hash_table用来解决拉链地址冲突。key_data[1]被分配了连续的地址,用来存储key值
struct LRUHandle {
void* value;
void (*deleter)(const Slice&, void* value);
LRUHandle* next_hash;
LRUHandle* next;
LRUHandle* prev;
size_t charge; // TODO(opt): Only allow uint32_t?
size_t key_length;
// The hash of key(). Used for fast sharding and comparisons.
uint32_t hash;
// The number of external refs to this entry. The cache itself is not counted.
uint32_t refs;
enum Flags : uint8_t {
// Whether this entry is referenced by the hash table.
IN_CACHE = (1 << 0),
// Whether this entry is high priority entry.
IS_HIGH_PRI = (1 << 1),
// Whether this entry is in high-pri pool.
IN_HIGH_PRI_POOL = (1 << 2),
// Wwhether this entry has had any lookups (hits).
HAS_HIT = (1 << 3),
};
char key_data[1];
LRUHandleTable是个自建的Hash Table,通过key用来快速定位到LRUHandle,从而得到prev 还是next
class LRUHandleTable {
}
LRUCacheShard是LRU的核心数据结构, 包括了insert ,LookUp数据结构