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对象结构使用更方便;
老规矩直接上代码:
templateclass 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,