Exemplo n.º 1
0
/*
    after some specific time ,
    merge cache data with disk file and update every thread's cache
    then every thread has the same cache
*/
void CacheManagerThread::synchronizeGlobalCacheWithDisk()
{
#ifndef NDEBUG
    WRITE_STR(string(" start synchronize Global Cache With Disk ......."));
#endif
    Configure *pconf = Configure::getInstance();
    string cache_path = pconf->getConfigByName("cache_file_path");
    string home_path = pconf->getConfigByName("home_path");
#ifndef NDEBUG
    WRITE_STR(string(" 1st. merge the disk cache data to memory ......."));
#endif
    // 1st. merge the disk cache data to memory;
    ifstream ifs((home_path + cache_path).c_str());
    if (!(ifs.is_open()))
    {
        throw runtime_error("open cache_path file");
    }
    string line, keyword, pairword;
    int frequency;
    while (getline(ifs, line))
    {
        istringstream istr(line);
        istr >> keyword;
        vector<pair<string, int> > vec;
        while (istr >> pairword)
        {
            istr >> frequency;
            vec.push_back(make_pair(pairword, frequency));
        }
        CacheData data(vec);
        global_cache_map_.insert(make_pair(keyword, data));
    }
    ifs.close();
#ifndef NDEBUG
    WRITE_STR(string(" 2nd. write the cache data back to disk...... "));
#endif
    // 2nd. write the global cache data back to disk;
    ofstream ofs((home_path + cache_path).c_str());
    // file lock;
    if (!(ofs.is_open()))
    {
        throw runtime_error("open cache_path file");
    }
    for (Cache::cache_map_type::iterator iter = global_cache_map_.begin() ; iter != global_cache_map_.end(); ++iter)
    {
        ofs << (*iter).first << "\t";
        vector<pair<string, int> > vec = (*iter).second.getDataVec();
        for (vector<pair<string, int> >::iterator it = vec.begin(); it != vec.end(); ++it)
        {
            ofs << (*it).first << " " << (*it).second<<" ";
        }
        ofs << "\n";
    }
    ofs.close();
}
Exemplo n.º 2
0
void CacheManagerThread::run()
{
    while (true)
    {
#ifndef NDEBUG
        WRITE_STR("start updateCache .......");
#endif
        updateCache();
        // 每隔一段时间就更新所有cache
        Configure *pconf = Configure::getInstance();
        string s_time = pconf->getConfigByName("cache_manager_sleep_seconds");
        int sleep_time = atoi(s_time.c_str());
        sleep(sleep_time);
    }
}