400行代码实现本地Key-Value缓存,性能每秒几百万次,进程重启有效,LRU淘汰——HashTable

标签: 代码 key value | 发表时间:2013-12-11 01:11 | 作者:gdutliuyun827
出处:http://blog.csdn.net

[email protected]原创,转载请注明出处: http://blog.csdn.net/gdutliuyun827

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;
}


 

 

作者:gdutliuyun827 发表于2013-12-10 17:11:37 原文链接
阅读:57 评论:0 查看评论

相关 [代码 key value] 推荐:

Tair: 淘宝的key/value解决方案

- duxin - 若海的blog
今天我们对外开源了Tair,Tair是由淘宝开发的key/value解决方案,你可以在这里获取更多信息. Tair在淘宝有着大规模的应用,在你登录淘宝、查看商品详情页面、在淘江湖和好友“捣浆糊”等等时候,后面都在直接或间接的和Tair交互. Tair是一个分布式的key/value结构数据的解决方案,系统默认支持基于内存和文件的存储引擎,对应于通常我们所说的缓存和持久化存储.

400行代码实现本地Key-Value缓存,性能每秒几百万次,进程重启有效,LRU淘汰——HashTable

- - CSDN博客数据库推荐文章
[email protected]原创,转载请注明出处: http://blog.csdn.net/gdutliuyun827. Key-Value缓存有很多,用的较多的是memcache、redis,他们都是以独立服务的形式运行,在工作中有时需要嵌入一个本地的key-value缓存,当然已经有LevelDb等,但感觉还是太重量级了.

高性能key-value数据库nessDB介绍

- - NoSQLFan
nessDB是一个小巧、高性能、可嵌入式的key/value存储引擎,使用标准C开发,支持Linux, *BSD, OS X and Solaris等系统,无第三方库依赖. 本文来自nessDB作者@ BohuTANG 的投稿分享,推荐给大家. 同时nessDB还提供一个服务端,支持Redis的 PING, SET, MSET, GET, MGET, DEL, EXISTS, INFO, SHUTDOWN 命令,您可以使用任何一款Redis客户端来连接和操作nessDB.

[转][转]高性能key-value数据库nessDB介绍

- - heiyeluren的Blog
来源:http://blog.nosqlfan.com/html/3851.html . nessDB是一个小巧、高性能、可嵌入式的key/value存储引擎,使用标准C开发,支持Linux, *BSD, OS X and Solaris等系统,无第三方库依赖. 本文来自nessDB作者@ BohuTANG 的投稿分享,推荐给大家.

使用key/value数据库redis和TTSERVER的体会

- - 开源软件 - ITeye博客
redis是一个类似memcached的key/value存储系统,它支持存储的value类型相对较多,包括string(字符串)、 list(链表)、set(集合)和zset(有序集合). 在此基础上,redis支持各种不同方式的排序. 与memcached一样,为了保证效率,数据都是缓存在内存中.

镀金键盘帽:Gold Key

- Paul - 爱…稀奇~{新鲜:科技:创意:有趣}
传统上而言,判断一个人是不是暴发户,最主要不是看小三的数量,也不是看有没有玛莎拉蒂,最重要的是——看他有没有一颗金牙. 这在改革刚开放那会,一颗金灿灿的门牙,简直就是“老子很有钱”的代名词,可比名片管用多了~. 所以,从这个角度而言,如果你怀念着那个光辉的时代,并想给自己来点后现代的色彩,那么,一颗镀金键盘帽(Gold Key)就是必须的了~4美元一颗,这里有售:chihapaura.com,用来替换自己键盘上的数字“4”,那“仇恨”吸得,我kao,即便是美美也不过如是啊.

mysql中的ON DUPLICATE KEY UPDATE

- - haohtml's blog
INSERT INTO ON DUPLICATE KEY UPDATE 与 REPLACE INTO,两个命令可以处理重复键值问题,在实际上它之间有什么区别呢. 前提条件是这个表必须有一个唯一索引或主键. 1、REPLACE发现重复的先删除再插入,如果记录有多个字段,在插入的时候如果有的字段没有赋值,那么新插入的记录这些字段为空.

给钥匙加上套子:Key Keeper

- Chuyue - 爱…稀奇~{新鲜:科技:创意:有趣}
来自日本设计师青木亮作(Ryosaku Aoki)的创意,Key Keeper是给钥匙用的硅胶TT——能干嘛呢. 在平时,它可以一定程度地将钥匙的棱角包裹起来(兼具防尘的作用),避免运动或者玩闹时不小心戳到自己,或者划伤手机外壳,同时多样的色彩又会是漂亮的分类标签,一种颜色对应一种钥匙~并且丝毫不会影响到钥匙本来的功能,开门的时候握住一头,仍然能轻松地插进钥匙孔,这时,Key Keeper会缩回,等抽出钥匙的时候它又能自动弹回.

Cloud Key,躲在 U 盘里的云

- SotongDJ - 爱范儿 · Beats of Bits
看看 Goolge 建立了多少个数据中心. 搭建云服务,需要要众多的服务器吧. 然而位于旧金山的 Piston 推出新产品 Cloud Key,把云服务装进了 U 盘. 那么,Cloud Key 能否像 U 盘那样方便易用呢. 根据公司描述,当 Cloud Key 插进交换机后,只需要短短几分钟,用户就能够完成云计算平台 OpenStack 的设置.

用 InnoDB 時關於 PRIMARY KEY 的建議

- - Gea-Suan Lin's BLOG
Percona 的「 InnoDB scalability issues due to tables without primary keys」這篇文章在討論 InnoDB 在沒有 PRIMARY KEY 時的效能問題. 在討論效能問題前,應該先讀過 MySQL 官方文件裡提到 InnoDB index 架構的文章,其中就有提到 PRIMARY KEY 以及其他的 INDEX KEY 的底層架構:「 InnoDB Table and Index Structures」.