2015-04-15 19:05:58 +02:00
|
|
|
#include "Cache.h"
|
2015-04-18 16:50:19 +02:00
|
|
|
#include "PeriodicTask.h"
|
2015-04-15 19:05:58 +02:00
|
|
|
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
|
|
|
using std::mutex;
|
|
|
|
using std::lock_guard;
|
|
|
|
using std::pair;
|
|
|
|
|
|
|
|
namespace blockstore {
|
2015-04-16 14:10:44 +02:00
|
|
|
namespace caching {
|
2015-04-15 19:05:58 +02:00
|
|
|
|
|
|
|
constexpr uint32_t Cache::MAX_ENTRIES;
|
2015-04-18 16:50:19 +02:00
|
|
|
constexpr double Cache::PURGE_LIFETIME_SEC;
|
|
|
|
constexpr double Cache::PURGE_INTERVAL;
|
|
|
|
constexpr double Cache::MAX_LIFETIME_SEC;
|
|
|
|
|
|
|
|
Cache::Cache(): _cachedBlocks(), _timeoutFlusher(nullptr) {
|
|
|
|
//Don't initialize timeoutFlusher in the initializer list,
|
|
|
|
//because it then might already call Cache::popOldEntries() before Cache is done constructing
|
|
|
|
_timeoutFlusher = make_unique<PeriodicTask>(std::bind(&Cache::_popOldEntries, this), PURGE_INTERVAL);
|
2015-04-15 19:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Cache::~Cache() {
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ptr<Block> Cache::pop(const Key &key) {
|
|
|
|
lock_guard<mutex> lock(_mutex);
|
2015-04-15 20:39:58 +02:00
|
|
|
auto found = _cachedBlocks.pop(key);
|
|
|
|
if (found.get() == nullptr) {
|
2015-04-15 19:05:58 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-15 20:39:58 +02:00
|
|
|
auto block = found->releaseBlock();
|
2015-04-15 19:05:58 +02:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cache::push(unique_ptr<Block> block) {
|
|
|
|
lock_guard<mutex> lock(_mutex);
|
2015-04-15 20:39:58 +02:00
|
|
|
assert(_cachedBlocks.size() <= MAX_ENTRIES);
|
|
|
|
if (_cachedBlocks.size() == MAX_ENTRIES) {
|
|
|
|
_cachedBlocks.pop();
|
2015-04-15 19:05:58 +02:00
|
|
|
assert(_cachedBlocks.size() == MAX_ENTRIES-1);
|
|
|
|
}
|
|
|
|
Key key = block->key();
|
2015-04-15 20:39:58 +02:00
|
|
|
_cachedBlocks.push(key, make_unique<CacheEntry>(std::move(block)));
|
2015-04-15 19:05:58 +02:00
|
|
|
}
|
|
|
|
|
2015-04-18 16:50:19 +02:00
|
|
|
void Cache::_popOldEntries() {
|
|
|
|
lock_guard<mutex> lock(_mutex);
|
|
|
|
while(_cachedBlocks.size() > 0 && _cachedBlocks.peek().ageSeconds() > PURGE_LIFETIME_SEC) {
|
|
|
|
_cachedBlocks.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:05:58 +02:00
|
|
|
}
|
|
|
|
}
|