]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mgr/TTLCache.h
15 template <class Key
, class Value
> class Cache
{
17 std::atomic
<uint64_t> hits
, misses
;
20 unsigned int capacity
;
21 Cache(unsigned int size
= UINT16_MAX
) : hits
{0}, misses
{0}, capacity
{size
} {};
22 std::map
<Key
, Value
> content
;
23 std::vector
<string
> allowed_keys
= {"osd_map", "pg_dump", "pg_stats"};
33 unsigned int get_misses() { return misses
; }
34 unsigned int get_hits() { return hits
; }
35 void throw_key_not_found(Key key
) {
37 ss
<< "Key " << key
<< " couldn't be found\n";
38 throw std::out_of_range(ss
.str());
42 void insert(Key key
, Value value
) {
44 if (content
.size() < capacity
) {
45 content
.insert({key
, value
});
48 Value
get(Key key
, bool count_hit
= true) {
54 void erase(Key key
) { content
.erase(content
.find(key
)); }
55 void clear() { content
.clear(); }
56 bool exists(Key key
) { return content
.find(key
) != content
.end(); }
57 std::pair
<uint64_t, uint64_t> get_hit_miss_ratio() {
58 return std::make_pair(hits
.load(), misses
.load());
60 bool is_cacheable(Key key
) {
61 for (auto k
: allowed_keys
) {
62 if (key
== k
) return true;
66 int size() { return content
.size(); }
71 using ttl_time_point
= std::chrono::time_point
<std::chrono::steady_clock
>;
72 template <class Key
, class Value
>
73 class TTLCacheBase
: public Cache
<Key
, std::pair
<Value
, ttl_time_point
>> {
76 float ttl_spread_ratio
;
77 using value_type
= std::pair
<Value
, ttl_time_point
>;
78 using cache
= Cache
<Key
, value_type
>;
81 Value
get_value(Key key
, bool count_hit
= true);
82 ttl_time_point
get_value_time_point(Key key
);
84 bool expired(Key key
);
85 void finish_get(Key key
);
86 void finish_erase(Key key
);
87 void throw_key_not_found(Key key
);
90 TTLCacheBase(uint16_t ttl_
= 0, uint16_t size
= UINT16_MAX
,
92 : Cache
<Key
, value_type
>(size
), ttl
{ttl_
}, ttl_spread_ratio
{spread
} {}
94 void insert(Key key
, Value value
);
98 uint16_t get_ttl() { return ttl
; };
99 void set_ttl(uint16_t ttl
);
102 template <class Key
, class Value
>
103 class TTLCache
: public TTLCacheBase
<Key
, Value
> {
105 TTLCache(uint16_t ttl_
= 0, uint16_t size
= UINT16_MAX
, float spread
= 0.25)
106 : TTLCacheBase
<Key
, Value
>(ttl_
, size
, spread
) {}
111 class TTLCache
<Key
, PyObject
*> : public TTLCacheBase
<Key
, PyObject
*> {
113 TTLCache(uint16_t ttl_
= 0, uint16_t size
= UINT16_MAX
, float spread
= 0.25)
114 : TTLCacheBase
<Key
, PyObject
*>(ttl_
, size
, spread
) {}
116 PyObject
* get(Key key
);
120 using ttl_base
= TTLCacheBase
<Key
, PyObject
*>;
123 #include "TTLCache.cc"