-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlru.cpp
34 lines (30 loc) · 810 Bytes
/
lru.cpp
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
#include "lru.h"
int LRUCache::get(int target) {
if (_capacity <= 0) {
return -1;
}
++_get_count;
auto it = _table.find(target);
if (it != _table.end()) {
++_hit_count;
_items.splice(_items.begin(), _items, it->second);
return _items.front().second;
} else {
if (_items.size() >= _capacity) {
_table.erase(_items.back().first);
_items.pop_back();
}
_items.emplace_front(target, target);
_table[target] = _items.begin();
return _items.front().second;
}
}
std::string LRUCache::statics() {
std::stringstream s;
s << "trace:" << _file_name << " lru_cache:"
<< " cache_size:" << _capacity
<< " request:" << _get_count
<< " hit:" << _hit_count
<< " hit_rate:" << 1.0 * _hit_count / _get_count << std::endl;
return s.str();
}