libcryfs/implementations/caching2/Cache.cpp

43 lines
855 B
C++
Raw Normal View History

#include "Cache.h"
using std::unique_ptr;
using std::make_unique;
using std::mutex;
using std::lock_guard;
using std::pair;
namespace blockstore {
namespace caching2 {
constexpr uint32_t Cache::MAX_ENTRIES;
Cache::Cache(): _cachedBlocks() {
}
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) {
return nullptr;
}
2015-04-15 20:39:58 +02:00
auto block = found->releaseBlock();
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();
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)));
}
}
}