-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcache.h
55 lines (47 loc) · 1.22 KB
/
bcache.h
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
#ifndef BCACHE_H
#define BCACHE_H
#include <map>
class cache_traits {
};
template<typename ItemType, typename CacheTraits>
class BCache {
public:
typedef unsigned int ID;
typedef std::map<ID, ItemType> CACHE_STORAGE;
CACHE_STORAGE cacheItems;
BCache() : id(0) {}
ID InsertIntoCache(ItemType item) {
cacheItems[id] = item;
return id++;
}
ItemType GetRecord(ID id) {
ItemType cacheItem = cacheItems[id];
if (!cacheItem->IsCached()) {
cacheItem->LoadIntoCache();
}
return cacheItem;
}
void ClearCache() {
CACHE_STORAGE::iterator i = cacheItems.begin();
while (i != cacheItems.end()) {
if ((*i)->IsCached()) {
(*i)->DeleteFromCache();
}
delete *i;
}
}
void DestroyRecord(ID id) {
auto toDelete = cacheItems.find(id);
if (toDelete != cacheItems.end()) {
if (toDelete->second->IsCached()) {
toDelete->second->DeleteFromCache();
}
cacheItems.erase(toDelete);
} else {
std::cerr << "ID: " << id << " missed" << std::endl;
}
}
private:
ID id;
};
#endif // BCACHE_H