Home  >  Article  >  Database  >  [Leveldb]源码分析之二 Cache模块的实现

[Leveldb]源码分析之二 Cache模块的实现

WBOY
WBOYOriginal
2016-06-07 17:37:581088browse

在Leveldb中,用户可以将自定义的Cache逻辑传入Option中,默认使用的是LRU算法实现,下面是实现类图: 其中LRUHandle是存放数据的node LRUCache实现具体的Insert,lookup等操作,而ShardedLRUCache中又实现分片包含多个LRUCache。 HandleTable是自己实现的一

在Leveldb中,用户可以将自定义的Cache逻辑传入Option中,默认使用的是LRU算法实现,,下面是实现类图:


其中LRUHandle是存放数据的node
LRUCache实现具体的Insert,lookup等操作,而ShardedLRUCache中又实现分片包含多个LRUCache。
HandleTable是自己实现的一个HashTable,官方说法是这个实现相较内部实现会有百分之五的提升。
下面是主要操作的流程:
Insert:
1.根据hash结果由ShardedLRUCache选择相应的LRUCache
2.将数据存入HashTable中,若空间不足则相应*2
3.如果是一个已存在的数据那么就根据LRU算法调整存储的位置

Lookup:

1.将key做hash寻找相应的LRUCache
2.在HandleTable中查找是否存在该数据,若存在则调整相应位置

个人博客:?p=72

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn