Key-Value缓存有很多,用的较多的是memcache、redis,他们都是以独立服务的形式运行,在工作中有时需要嵌入一个本地的key-value缓存,当然已经有LevelDb等,但感觉还是太重量级了。
本文实现了一种超级轻量的缓存,
1、实现代码仅仅需要400行;
2、性能高效,value长度在1K时测试速度在每秒200万左右
3、缓存是映射到文件中的,所以没有malloc、free的开销,以及带来的内存泄露、内存碎片等;
4、如果服务挂掉了,重启后缓存内容继续存在;
5、如果把缓存映射到磁盘文件就算机器挂了,缓存中内容还是会存在,当然有可能会出现数据损坏的情况;
6、一定程度上实现了LRU淘汰算法,实现的LRU不是全局的只是一条链上的,所以只能说在一定程序上实现了;
7、稳定,已经在多个项目中运用,线上部署的机器有几十台,运行了大半年了没出过问题;
8、普通的缓存key、value都是字符串的形式,此缓存的key、value都可以是class、struct对象结构使用更方便;
老规矩直接上代码:
复制代码 代码如下:
template<typename K, typename V>
class HashTable
{
public:
HashTable(const char *tablename, uint32_t tableLen, uint32_t nodeTotal);
virtual ~HashTable();
bool Add(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
//check is exist
uint32_t nodeId = GetIdByKey(key);
if(nodeId != m_InvalidId) return false;
nodeId = GetFreeNode();
if(nodeId == m_InvalidId) return false;
uint32_t hashCode = key.HashCode();
Entry *tmpNode = m_EntryAddr + nodeId;
tmpNode->m_Key = key;
tmpNode->m_Code = hashCode;
tmpNode->m_Value = value;
uint32_t index = hashCode % m_HeadAddr->m_TableLen;
AddNodeToHead(index, nodeId);
return true;
}
bool Del(K &key)
{
AutoLock autoLock(m_MutexLock);
uint32_t nodeId = GetIdByKey(key);
if(nodeId == m_InvalidId) return false;
uint32_t index = key.HashCode() % m_HeadAddr->m_TableLen;
return RecycleNode(index, nodeId);
}
bool Set(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
uint32_t nodeId = GetIdByKey(key);
if(nodeId == m_InvalidId) return false;
(m_EntryAddr + nodeId)->m_Value = value;
return true;
}
bool Get(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
uint32_t nodeId = GetIdByKey(key);
if(nodeId == m_InvalidId) return false;
value = (m_EntryAddr + nodeId)->m_Value;
return true;
}
bool Exist(K &key)
{
AutoLock autoLock(m_MutexLock);
uint32_t nodeId = GetIdByKey(key);
if(nodeId == m_InvalidId) return false;
return true;
}
uint32_t Count()
{
AutoLock autoLock(m_MutexLock);
return m_HeadAddr->m_UsedCount;
}
//if exist set else add
bool Replace(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
if(Exist(key)) return Set(key, value);
else return Add(key, value);
}
/***********************************************
****LRU: when visit a node, move it to head ****
************************************************/
//if no empty place,recycle tail
bool LruAdd(K &key, V &value, K &recyKey, V &recyValue, bool &recycled)
{
AutoLock autoLock(m_MutexLock);
if(Exist(key)) return false;
if(Add(key, value)) return true;
uint32_t index = key.HashCode() % m_HeadAddr->m_TableLen;
uint32_t tailId = GetTailNodeId(index);
if(tailId == m_InvalidId) return false;
Entry *tmpNode = m_EntryAddr + tailId;
recyKey = tmpNode->m_Key;
recyValue = tmpNode->m_Value;
recycled = true;
RecycleNode(index, tailId);
return Add(key, value);
}
bool LruSet(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
if(Set(key, value)) return MoveToHead(key);
else return false;
}
bool LruGet(K &key, V &value)
{
AutoLock autoLock(m_MutexLock);
if(Get(key, value)) return MoveToHead(key);
else return false;
}
//if exist set else add; if add failed recycle tail than add
bool LruReplace(K &key, V &value, K &recyKey, V &recyValue, bool &recycled)
{
AutoLock autoLock(m_MutexLock);
recycled = false;
if(Exist(key)) return LruSet(key, value);
else return LruAdd(key, value, recyKey, recyValue, recycled);
}
void Clear()
{
AutoLock autoLock(m_MutexLock);
m_HeadAddr->m_FreeBase = 0;
m_HeadAddr->m_RecycleHead = 0;
m_HeadAddr->m_UsedCount = 0;
for(uint32_t i = 0; i < m_HeadAddr->m_TableLen; ++i)
{
(m_ArrayAddr+i)->m_Head = m_InvalidId;
(m_ArrayAddr+i)->m_Tail = m_InvalidId;
}
}
int GetRowKeys(vector<K> &keys, uint32_t index)
{
AutoLock autoLock(m_MutexLock);
if(index >= m_HeadAddr->m_TableLen) return -1;
keys.clear();
keys.reserve(16);
int count = 0;
Array *tmpArray = m_ArrayAddr + index;
uint32_t nodeId = tmpArray->m_Head;
while(nodeId != m_InvalidId)
{
Entry *tmpNode = m_EntryAddr + nodeId;
keys.push_back(tmpNode->m_Key);
nodeId = tmpNode->m_Next;
++count;
}
return count;
}
void *Padding(uint32_t size)
{
AutoLock autoLock(m_MutexLock);
if(size > m_HeadSize - sizeof(TableHead)) return NULL;
else return m_HeadAddr->m_Padding;
}
private:
static const uint32_t m_InvalidId = 0xffffffff;
static const uint32_t m_HeadSize = 1024;
struct TableHead
{
uint32_t m_TableLen;
uint32_t m_NodeTotal;
uint32_t m_FreeBase;
uint32_t m_RecycleHead;
uint32_t m_UsedCount;
char m_TableName[256];
uint32_t m_Padding[0];
};
struct Array
{
uint32_t m_Head;
uint32_t m_Tail;
};
struct Entry
{
V m_Value;
K m_Key;
uint32_t m_Code;
uint32_t m_Next;
uint32_t m_Prev;
};
size_t m_MemSize;
uint8_t *m_MemAddr;
TableHead *m_HeadAddr;
Array *m_ArrayAddr;
Entry *m_EntryAddr;
ThreadMutex m_MutexLock;
bool MoveToHead(K &key);
uint32_t GetIdByKey(K &key);
void AddNodeToHead(uint32_t index, uint32_t nodeId);
bool MoveNodeToHead(uint32_t index, uint32_t nodeId);
bool RecycleNode(uint32_t index, uint32_t nodeId);
uint32_t GetTailNodeId(uint32_t index);
uint32_t GetFreeNode();
DISABLE_COPY_AND_ASSIGN(HashTable);
};
template<typename K, typename V>
HashTable<K, V>::HashTable(const char *tablename, uint32_t tableLen, uint32_t nodeTotal)
{
AbortAssert(tablename != NULL);
m_MemSize = m_HeadSize + tableLen*sizeof(Array) + nodeTotal*sizeof(Entry);
m_MemAddr = (uint8_t*)MemFile::Realloc(tablename, m_MemSize);
AbortAssert(m_MemAddr != NULL);
m_HeadAddr = (TableHead*)(m_MemAddr);
m_ArrayAddr = (Array*)(m_MemAddr + m_HeadSize);
m_EntryAddr = (Entry*)(m_MemAddr + m_HeadSize + tableLen*sizeof(Array));
m_HeadAddr->m_TableLen = tableLen;
m_HeadAddr->m_NodeTotal = nodeTotal;
strncpy(m_HeadAddr->m_TableName, tablename, sizeof(m_HeadAddr->m_TableName));
if(m_HeadAddr->m_UsedCount == 0)//if first use init array to invalid id
{
for(uint32_t i = 0; i < tableLen; ++i)
{
(m_ArrayAddr+i)->m_Head = m_InvalidId;
(m_ArrayAddr+i)->m_Tail = m_InvalidId;
}
m_HeadAddr->m_FreeBase = 0;
m_HeadAddr->m_RecycleHead = 0;
}
}
template<typename K, typename V>
HashTable<K, V>::~HashTable()
{
MemFile::Release(m_MemAddr, m_MemSize);
}
template<typename K, typename V>
bool HashTable<K, V>::MoveToHead(K &key)
{
uint32_t nodeId = GetIdByKey(key);
uint32_t index = key.HashCode() % m_HeadAddr->m_TableLen;
return MoveNodeToHead(index, nodeId);
}
template<typename K, typename V>
uint32_t HashTable<K, V>::GetIdByKey(K &key)
{
uint32_t hashCode = key.HashCode();
uint32_t index = hashCode % m_HeadAddr->m_TableLen;
Array *tmpArray = m_ArrayAddr + index;
uint32_t nodeId = tmpArray->m_Head;
while(nodeId != m_InvalidId)
{
Entry *tmpNode = m_EntryAddr + nodeId;
if(tmpNode->m_Code == hashCode && key.Equals(tmpNode->m_Key)) break;
nodeId = tmpNode->m_Next;
}
return nodeId;
}
template<typename K, typename V>
void HashTable<K, V>::AddNodeToHead(uint32_t index, uint32_t nodeId)
{
if(index >= m_HeadAddr->m_TableLen || nodeId >= m_HeadAddr->m_NodeTotal) return;
Array *tmpArray = m_ArrayAddr + index;
Entry *tmpNode = m_EntryAddr + nodeId;
if(m_InvalidId == tmpArray->m_Head)
{
tmpArray->m_Head = nodeId;
tmpArray->m_Tail = nodeId;
}
else
{
tmpNode->m_Next = tmpArray->m_Head;
(m_EntryAddr + tmpArray->m_Head)->m_Prev = nodeId;
tmpArray->m_Head = nodeId;
}
}
template<typename K, typename V>
bool HashTable<K, V>::MoveNodeToHead(uint32_t index, uint32_t nodeId)
{
if(index >= m_HeadAddr->m_TableLen || nodeId >= m_HeadAddr->m_NodeTotal) return false;
Array *tmpArray = m_ArrayAddr + index;
Entry *tmpNode = m_EntryAddr + nodeId;
//already head
if(tmpArray->m_Head == nodeId)
{
return true;
}
uint32_t nodePrev = tmpNode->m_Prev;
uint32_t nodeNext = tmpNode->m_Next;
(m_EntryAddr+nodePrev)->m_Next = nodeNext;
if(nodeNext != m_InvalidId)
{
(m_EntryAddr+nodeNext)->m_Prev = nodePrev;
}
else
{
tmpArray->m_Tail = nodePrev;
}
tmpNode->m_Prev = m_InvalidId;
tmpNode->m_Next = tmpArray->m_Head;
(m_EntryAddr + tmpArray->m_Head)->m_Prev = nodeId;
tmpArray->m_Head = nodeId;
return true;
}
template<typename K, typename V>
bool HashTable<K, V>::RecycleNode(uint32_t index, uint32_t nodeId)
{
if(index >= m_HeadAddr->m_TableLen || nodeId >= m_HeadAddr->m_NodeTotal) return false;
Array *tmpArray = m_ArrayAddr + index;
Entry *tmpNode = m_EntryAddr + nodeId;
uint32_t nodePrev = tmpNode->m_Prev;
uint32_t nodeNext = tmpNode->m_Next;
if(nodePrev != m_InvalidId)
{
(m_EntryAddr + nodePrev)->m_Next = nodeNext;
}
else
{
tmpArray->m_Head = nodeNext;
}
if(nodeNext != m_InvalidId)
{
(m_EntryAddr + nodeNext)->m_Prev = nodePrev;
}
else
{
tmpArray->m_Tail = nodePrev;
}
(m_EntryAddr+nodeId)->m_Next = m_HeadAddr->m_RecycleHead;
m_HeadAddr->m_RecycleHead = nodeId;
--(m_HeadAddr->m_UsedCount);
return true;
}
template<typename K, typename V>
uint32_t HashTable<K, V>::GetTailNodeId(uint32_t index)
{
if(index >= m_HeadAddr->m_TableLen) return m_InvalidId;
Array *tmpArray = m_ArrayAddr + index;
return tmpArray->m_Tail;
}
template<typename K, typename V>
uint32_t HashTable<K, V>::GetFreeNode()
{
uint32_t nodeId = m_InvalidId;
if(m_HeadAddr->m_UsedCount < m_HeadAddr->m_FreeBase)//get from recycle list
{
nodeId = m_HeadAddr->m_RecycleHead;
m_HeadAddr->m_RecycleHead = (m_EntryAddr+nodeId)->m_Next;
++(m_HeadAddr->m_UsedCount);
}
else if(m_HeadAddr->m_UsedCount < m_HeadAddr->m_NodeTotal)//get from free mem
{
nodeId = m_HeadAddr->m_FreeBase;
++(m_HeadAddr->m_FreeBase);
++(m_HeadAddr->m_UsedCount);
}
else
{
nodeId = m_InvalidId;
}
//init node
if(nodeId < m_HeadAddr->m_NodeTotal)
{
Entry *tmpNode = m_EntryAddr + nodeId;
memset(tmpNode, 0, sizeof(Entry));
tmpNode->m_Next = m_InvalidId;
tmpNode->m_Prev = m_InvalidId;
}
return nodeId;
}
keyvalue数据库
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]