13#include "detail/tick_service.h"
16 template<
typename K,
typename T>
23 using TickType = TickService::TickType;
24 using IndexType = std::uint32_t;
26 using BucketType = std::vector<K>;
27 using ValueType = std::shared_ptr<T>;
28 using CacheType = std::map<K, ValueType>;
31 Cache(
size_t duration,
size_t interval, std::shared_ptr<TickService> tick_service)
37 if (duration == 0 || duration % interval != 0 || duration == interval) {
38 throw std::invalid_argument(
"Invalid time_queue constructor args");
42 throw std::invalid_argument(
"Tick service cannot be null");
68 bool Contains(
const K& start_key,
const K& end_key)
70 if (start_key >= end_key) {
71 throw std::invalid_argument(
"Exclusive end key must be greater than start key");
76 for (
auto key = start_key; key < end_key; ++key) {
85 ValueType
Get(
const K& key)
noexcept
94 std::vector<ValueType>
Get(
const K& start_key,
const K& end_key)
101 std::vector<ValueType> entries(end_key - start_key,
nullptr);
102 for (
auto key = start_key; key < end_key; ++key) {
103 entries[key - start_key] =
cache_.at(key);
117 return std::begin(
cache_)->second;
128 return std::prev(std::end(
cache_))->second;
157 for (TickType i = 0; i < delta; ++i) {
159 for (
const auto& key : bucket) {
168 template<
typename Value>
172 throw std::invalid_argument(
"TTL is greater than max duration");
173 }
else if (ttl == 0) {
182 buckets_[future_index].push_back(key);
183 cache_[key] = std::make_shared<T>(value);
const size_t total_buckets_
The total amount of buckets. Value is calculated by duration / interval.
Definition cache.h:194
void Clear() noexcept
Definition cache.h:131
void Insert(const K &key, T &&value, size_t ttl)
Definition cache.h:60
std::shared_ptr< TickService > tick_service_
Tick service for calculating new tick and jumps in time.
Definition cache.h:209
TickType current_ticks_
Last calculated tick value.
Definition cache.h:200
const size_t duration_
The duration in ticks of the entire queue.
Definition cache.h:188
ValueType Get(const K &key) noexcept
Definition cache.h:85
Cache(size_t duration, size_t interval, std::shared_ptr< TickService > tick_service)
Definition cache.h:31
CacheType cache_
The cache of elements being stored.
Definition cache.h:206
void Insert(const K &key, const T &value, size_t ttl)
Definition cache.h:58
bool Contains(const K &start_key, const K &end_key)
Definition cache.h:68
IndexType bucket_index_
The index in time of the current bucket.
Definition cache.h:197
bool Contains(const K &key) noexcept
Definition cache.h:62
ValueType First() noexcept
Definition cache.h:109
size_t Size() const noexcept
Definition cache.h:55
Cache(Cache &&) noexcept=default
Cache(const Cache &)=default
std::vector< ValueType > Get(const K &start_key, const K &end_key)
Definition cache.h:94
ValueType Last() noexcept
Definition cache.h:120
bool Empty() const noexcept
Definition cache.h:56
void InternalInsert(const K &key, Value value, size_t ttl)
Definition cache.h:169
void Advance()
Definition cache.h:142
const size_t interval_
The interval at which buckets are cleared in ticks.
Definition cache.h:191
std::vector< BucketType > buckets_
The memory storage for all keys to be managed.
Definition cache.h:203
Definition transport.h:28