先講單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數據結構