Volume Cartographer 2.27.0
Cache.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cstddef>
6#include <functional>
7
8namespace volcart
9{
16template <typename TKey, typename TValue>
17class Cache
18{
19public:
21 virtual ~Cache() = default;
22
24 using Pointer = std::shared_ptr<Cache>;
25
28 virtual void setCapacity(std::size_t newCapacity) = 0;
29
31 [[nodiscard]] virtual auto capacity() const -> std::size_t = 0;
32
34 [[nodiscard]] virtual auto size() const -> std::size_t = 0;
39 virtual auto get(const TKey& k) -> TValue = 0;
40
42 virtual auto put(const TKey& k, const TValue& v) -> void = 0;
43
45 virtual auto contains(const TKey& k) -> bool = 0;
46
48 virtual void onEvict(std::function<bool(TKey&, TValue&)> fn) = 0;
49
51 virtual void resetOnEvict() = 0;
52
54 virtual auto evict() -> void = 0;
55
57 virtual auto purge() -> void = 0;
60protected:
62 Cache() = default;
63
65 explicit Cache(const std::size_t capacity) : capacity_{capacity} {}
66
68 std::size_t capacity_{200};
69
71 std::function<bool(TKey&, TValue&)> on_eject_;
72};
73} // namespace volcart
Abstract Base Class for Key-Value Caches.
Definition: Cache.hpp:18
virtual auto put(const TKey &k, const TValue &v) -> void=0
Put an item into the cache.
virtual auto purge() -> void=0
Evict all items ignoring cache policy.
virtual auto get(const TKey &k) -> TValue=0
Get an item from the cache by key.
std::function< bool(TKey &, TValue &)> on_eject_
Definition: Cache.hpp:71
std::size_t capacity_
Definition: Cache.hpp:68
virtual void resetOnEvict()=0
Remove the validation callback function.
virtual void onEvict(std::function< bool(TKey &, TValue &)> fn)=0
Callback function when items are evicted.
virtual ~Cache()=default
std::shared_ptr< Cache > Pointer
Definition: Cache.hpp:24
virtual auto contains(const TKey &k) -> bool=0
Check if an item is already in the cache.
virtual auto size() const -> std::size_t=0
Get the current number of elements in the cache.
virtual void setCapacity(std::size_t newCapacity)=0
Set the maximum number of elements in the cache.
virtual auto evict() -> void=0
Evict items following the cache policy.
virtual auto capacity() const -> std::size_t=0
Get the maximum number of elements in the cache.
Volume Cartographer library