• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            red22

            通過STL實(shí)現(xiàn)帶LRU的Cache

                    開發(fā)互聯(lián)網(wǎng)的server系統(tǒng),難免會用一些cache機(jī)制,通常在Linux下多使用共享內(nèi)存機(jī)制,把分配的內(nèi)存組織成一個特定的數(shù)據(jù)結(jié)構(gòu),用來緩存后端DB的數(shù)據(jù),加速讀寫。但是對一些新手而言,共享內(nèi)存的使用還是有一些門檻,與此同時STL的使用卻是相對容易上手,所以可以通過STL的容器組合出一個cache來。

                    上網(wǎng)搜了一下,看到已經(jīng)有人做了一些類似的事情,具體可以參看http://bytes.com/forum/thread640146.html,作者采用STL::map(當(dāng)然也可以是hash_map做數(shù)據(jù)緩存cache),采用一個list做lru鏈,每當(dāng)讀寫cache中的數(shù)據(jù)時,都要在lru鏈中刪除該key值對應(yīng)的項(xiàng),并把該數(shù)據(jù)的key值重新放到頭部,這里面有個相對較慢的地方就是從lru鏈中刪除key值對應(yīng)的項(xiàng)的操作,因?yàn)閯h除操作表面看也僅是erase,但是其中也是順序查找出來再刪,相對耗時。可以針對此對之進(jìn)行改進(jìn)。

                     采用STL::map做lru鏈,map::first是一個“虛時間”,表示訪問某一個key的虛時間,map::second就是key值,同樣另外一個map做cache緩存數(shù)據(jù),first為key,second為(value,virtual_time)對,這樣當(dāng)讀寫一個key的數(shù)據(jù)時,可以快速定位到該數(shù)據(jù),并且可以查找到它的上次訪問時間,從而在lru鏈里面快速定位(樹上的查找終究要快)并刪除,再更新key的訪問時間并插入到lru鏈中就可以了。

                     當(dāng)cache過大了,比如緩存最多100,那么就可以從lru鏈的頭部開始淘汰了,(因?yàn)閘ru鏈first值越小,表示訪問越久了)。代碼貼在下面,歡迎批評指證。與上面文章里的相比,代碼簡化了給多,這樣比較方便閱讀,另外考慮到數(shù)據(jù)多是blob,所以cache中的value就采用stl::string了。

                     STL做cache的幾個缺點(diǎn):

                    1 由于cache的數(shù)據(jù)在堆棧中,所以server一旦core掉就丟了;

                    2 鑒于上述原因,所以這種cache適合“讀多寫少”的業(yè)務(wù),一旦有“寫入”操作,就和數(shù)據(jù)庫同步更新,這樣的話淘汰的時候也比較簡單,直接丟掉就好了,不存在“臟數(shù)據(jù)”的概念。

                    當(dāng)然,對于海量用戶訪問的業(yè)務(wù),還是建議用共享內(nèi)存做cache機(jī)制,這樣讀寫的效率都會提高,并且程序出問題數(shù)據(jù)仍然在,但是壞處就是會帶來復(fù)雜性和好多其它的問題,就不在這里說了。

            #ifndef _MAP_LRU_CACHE_H_
            #define _MAP_LRU_CACHE_H_

            #include <string.h>

            #include <iostream>
            #include <list>
            #include <map>

            using namespace std;

            namespace lru_cache {

                    static const int DEF_CAPACITY = 100000;

                    typedef unsigned long long virtual_time;

                    typedef struct _HashKey
                    {// key的類型自定義,重要的是要overload <和==

                    }HashKey;

                    typedef struct _HashValue
                    {
                            string value_;
                            virtual_time access_;
                    }HashValue;


                    class CLRUCache
                    {
                            public:

                                    CLRUCache() : _lru_list(), _hash_table(), _now(0){}
                                    virtual ~CLRUCache(){}

                                    int set( const HashKey& key, const string &value );
                                    HashValue* get( const HashKey& key );


                                    unsigned get_lru_list_size(){ return (unsigned)_lru_list.size(); }
                                    unsigned get_hash_table_size() { return (unsigned)_hash_table.size(); }
                                    virtual_time get_now() { return _now; }

                             private:

                                    virtual_time get_virtual_time()
                                    {
                                            return ++_now;
                                    }

                                    map<virtual_time, HashKey>    _lru_list;
                                    map<HashKey, HashValue> _hash_table;
                                    virtual_time _now;
                    };

            }
            #endif

            #include "map_lru_cache.h"

            using namespace lru_cache;

            int CLRUCache::set( const HashKey& key, const string &value )
            {
                    HashValue hash_value;
                    hash_value.value_ = value;
                    hash_value.access_ = get_virtual_time();

                    pair< map<HashKey, HashValue>::iterator, bool > ret = _hash_table.insert(make_pair(key, hash_value));

                    if ( !ret.second )
                    {
                            // key already exist
                            virtual_time old_access = _hash_table[key].access_;
                            map<virtual_time, HashKey>::iterator iter = _lru_list.find(old_access);
                            if(iter != _lru_list.end())
                            {
                                    _lru_list.erase(iter);
                            }

                            _lru_list.insert(make_pair(hash_value.access_, key));
                            _hash_table[key] = hash_value;


                    }

                    else
                    {
                            _lru_list.insert(make_pair(hash_value.access_, key));

                            if ( _hash_table.size() > DEF_CAPACITY ) //ÌÔÌ­
                            {
                                    // get the least recently used key
                                    map<virtual_time, HashKey>::iterator iter = _lru_list.begin();


                                    _hash_table.erase( iter->second );
                                    // remove last key from list
                                    _lru_list.erase(iter);
                            }
                    }
                    return 0;
            }

            HashValue* CLRUCache::get( const HashKey& key )
            {
                    map<HashKey, HashValue>::iterator iter = _hash_table.find(key);
                    if ( iter != _hash_table.end() )
                    {
                            virtual_time old_access = iter->second.access_;
                            iter->second.access_ = get_virtual_time();

                            map<virtual_time, HashKey>::iterator it = _lru_list.find(old_access);
                            if(it != _lru_list.end())
                            {
                                    _lru_list.erase(it);
                            }

                            _lru_list.insert(make_pair(iter->second.access_, key));

                            return &(iter->second);
                    }

                    else
                            return NULL;
            }

            posted on 2008-09-22 15:21 red22 閱讀(3628) 評論(1)  編輯 收藏 引用

            Feedback

            # re: 通過STL實(shí)現(xiàn)帶LRU的Cache 2015-01-17 15:01 后知后覺

            基于C++實(shí)現(xiàn)的LRU的cache  回復(fù)  更多評論   



            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            My Links

            Blog Stats

            常用鏈接

            留言簿

            文章檔案

            搜索

            最新評論

            久久香蕉国产线看观看乱码| 国产aⅴ激情无码久久| 一级做a爰片久久毛片16| 国产成人香蕉久久久久| 亚洲人成无码www久久久| 午夜精品久久久久久久久| 九九99精品久久久久久| 亚洲乱码日产精品a级毛片久久| 亚洲精品无码久久久久sm| 国产成人99久久亚洲综合精品| 久久婷婷五月综合色奶水99啪| 久久久久久久尹人综合网亚洲| 四虎久久影院| 精品国产91久久久久久久a| 色诱久久久久综合网ywww| 亚洲精品久久久www| 久久激情五月丁香伊人| 久久精品国产亚洲av麻豆色欲 | 亚洲人成网亚洲欧洲无码久久| 亚洲精品午夜国产VA久久成人| 久久久久一本毛久久久| 国产精品九九九久久九九 | 国产高潮国产高潮久久久| 人妻丰满?V无码久久不卡| 94久久国产乱子伦精品免费| 久久精品无码午夜福利理论片| 久久精品国产亚洲av麻豆蜜芽| 久久天天婷婷五月俺也去| 久久免费观看视频| 久久久久99精品成人片牛牛影视| 国产亚洲欧美精品久久久| 欧洲人妻丰满av无码久久不卡| 国内精品人妻无码久久久影院导航| 色综合久久88色综合天天 | 久久中文字幕一区二区| 97超级碰碰碰碰久久久久| 色综合久久精品中文字幕首页| 国产美女久久久| 国内精品久久久久久久coent| 中文字幕亚洲综合久久| 久久久WWW成人免费毛片|