File size: 2,234 Bytes
3fd1a35 | 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 | #pragma once
#include <cstddef>
#include <list>
#include <string>
#include <unordered_map>
#include <utility>
namespace ling3 {
// Caller serializes access. Values should own immutable snapshots, so an active
// reader can safely retain a copy even after the cache releases its reference.
template<class T> class StateCache {
struct Entry { std::string key; T value; std::size_t bytes; };
using Iterator = typename std::list<Entry>::iterator;
std::list<Entry> entries_;
std::unordered_map<std::string, Iterator> index_;
std::size_t limit_, bytes_ = 0, evictions_ = 0;
public:
explicit StateCache(std::size_t limit) : limit_(limit) {}
StateCache(const StateCache &) = delete;
StateCache & operator=(const StateCache &) = delete;
StateCache(StateCache &&) = default;
StateCache & operator=(StateCache &&) = default;
const T * Get(const std::string & key) {
auto found = index_.find(key);
if (found == index_.end()) return nullptr;
entries_.splice(entries_.begin(), entries_, found->second);
return &found->second->value;
}
bool Erase(const std::string & key) {
auto found = index_.find(key);
if (found == index_.end()) return false;
bytes_ -= found->second->bytes; entries_.erase(found->second); index_.erase(found);
return true;
}
bool Put(std::string key, T value, std::size_t bytes) {
// An oversized update must not leave an old version under the same key.
Erase(key);
if (!limit_ || bytes > limit_) return false;
while (!entries_.empty() && bytes_ > limit_-bytes) {
Erase(entries_.back().key); ++evictions_;
}
entries_.push_front({std::move(key), std::move(value), bytes});
try { index_.emplace(entries_.front().key, entries_.begin()); }
catch (...) { entries_.pop_front(); throw; }
bytes_ += bytes; return true;
}
void Clear() { index_.clear(); entries_.clear(); bytes_ = 0; }
std::size_t bytes() const { return bytes_; }
std::size_t limit() const { return limit_; }
std::size_t size() const { return entries_.size(); }
std::size_t evictions() const { return evictions_; }
};
} // namespace ling3
|