-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemCache.cc
59 lines (50 loc) · 1.04 KB
/
MemCache.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
///
/// @file MemCache.cc
/// @author sgzed([email protected])
/// @date 2018-03-27 20:52:27
///
#include "MemCache.h"
#include <iostream>
using std::cout;
using std::endl;
MemCache::MemCache(size_t capacity)
:_capacity(capacity)
{
}
void MemCache::addCache(const pair<string,string>& element)
{
auto iter = _querys.find(element.first);
if(iter !=_querys.end())
{
_cache.erase(iter->second);
_cache.push_front(element);
_querys[element.first] = _cache.begin();
}
else
{
if(_cache.size()==_capacity)
{
_cache.pop_back();
}
_cache.push_front(element);
_querys[element.first] = _cache.begin();
}
}
string MemCache::query(const string& query)
{
string res;
cout << "MemCache::query :" << query << endl;
auto iter = _querys.find(query);
if(iter!=_querys.end())
res = iter->second->second;
cout << "MemCache::res :" << res << "#" << endl;
return res;
}
void MemCache::reNew()
{
for(auto & iter : _cache)
{
cout << iter.first << ":" << iter.second << endl;;
}
cout << " ----------------------- " << endl;
}