forked from grame-cncm/faustservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUSessionsCache.cpp
55 lines (50 loc) · 1.84 KB
/
LRUSessionsCache.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "LRUSessionsCache.hh"
extern int gVerbosity;
// LRUSessionsCache : a system that limits the number of cached sessions
LRUSessionsCache::LRUSessionsCache(const fs::path& aSessionsDir, int aMaxSize)
: fSessionsDir(aSessionsDir), fMaxSize(aMaxSize)
{
try {
// init the cache using the actual sessions in the sessions directory
if (fs::is_directory(fSessionsDir)) {
for (const auto& session : fs::directory_iterator(fSessionsDir)) {
refer(session.path().filename());
}
}
} catch (const boost::filesystem::filesystem_error& e) {
if (gVerbosity >= 2)
std::cerr << "Warning : sessions directory " << fSessionsDir << " doesn't exist yet " << e.code().message()
<< std::endl;
}
}
/* Refers key x with in the LRU cache */
void LRUSessionsCache::refer(const fs::path& x)
{
if (gVerbosity >= 2) std::cerr << "REFER TO " << x << std::endl;
// not present in cache
if (fSessionPos.find(x) == fSessionPos.end()) {
// cache is full
if (fSessionsList.size() == fMaxSize) {
// delete least recently used element
fs::path last = fSessionsList.back();
fSessionsList.pop_back();
fSessionPos.erase(last);
dispose(last);
}
} else {
fSessionsList.erase(fSessionPos[x]);
}
// update reference
fSessionsList.push_front(x);
fSessionPos[x] = fSessionsList.begin();
}
// dispose an element that leave the cache
void LRUSessionsCache::dispose(const fs::path& s)
{
if (gVerbosity >= 2) std::cerr << "DISPOSE OF " << s << std::endl;
try {
fs::remove_all(fSessionsDir / s);
} catch (const boost::filesystem::filesystem_error& e) {
std::cerr << "ERROR REMOVING CACHE ENTRY " << s << " : " << e.code().message() << std::endl;
}
}