#include "TTLCache.h" #include #include #include #include "PyUtil.h" template void TTLCacheBase::insert(Key key, Value value) { auto now = std::chrono::steady_clock::now(); if (!ttl) return; int16_t random_ttl_offset = ttl * ttl_spread_ratio * (2l * rand() / float(RAND_MAX) - 1); // in order not to have spikes of misses we increase or decrease by 25% of // the ttl int16_t spreaded_ttl = ttl + random_ttl_offset; auto expiration_date = now + std::chrono::seconds(spreaded_ttl); cache::insert(key, {value, expiration_date}); } template Value TTLCacheBase::get(Key key) { if (!exists(key)) { throw_key_not_found(key); } if (expired(key)) { erase(key); throw_key_not_found(key); } Value value = {get_value(key)}; return value; } template PyObject* TTLCache::get(Key key) { if (!this->exists(key)) { this->throw_key_not_found(key); } if (this->expired(key)) { this->erase(key); this->throw_key_not_found(key); } PyObject* cached_value = this->get_value(key); Py_INCREF(cached_value); return cached_value; } template void TTLCacheBase::erase(Key key) { cache::erase(key); } template void TTLCache::erase(Key key) { Py_DECREF(this->get_value(key, false)); ttl_base::erase(key); } template bool TTLCacheBase::expired(Key key) { ttl_time_point expiration_date = get_value_time_point(key); auto now = std::chrono::steady_clock::now(); if (now >= expiration_date) { return true; } else { return false; } } template void TTLCacheBase::clear() { cache::clear(); } template Value TTLCacheBase::get_value(Key key, bool count_hit) { value_type stored_value = cache::get(key, count_hit); Value value = std::get<0>(stored_value); return value; } template ttl_time_point TTLCacheBase::get_value_time_point(Key key) { value_type stored_value = cache::get(key, false); ttl_time_point tp = std::get<1>(stored_value); return tp; } template void TTLCacheBase::set_ttl(uint16_t ttl) { this->ttl = ttl; } template bool TTLCacheBase::exists(Key key) { return cache::exists(key); } template void TTLCacheBase::throw_key_not_found(Key key) { cache::throw_key_not_found(key); }