userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
lru_set.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/cache/lru_set.hpp
4
/// @brief @copybrief cache::LruSet
5
6
#
include
<
userver
/
cache
/
impl
/
lru
.
hpp
>
7
8
USERVER_NAMESPACE_BEGIN
9
10
namespace
cache
{
11
12
/// @ingroup userver_universal userver_containers
13
///
14
/// LRU set, thread safety matches Standard Library thread safety
15
template
<
16
typename
T,
17
typename
Hash = std::hash<impl::StringToStringView<T>>,
18
typename
Equal = std::equal_to<impl::StringToStringView<T>>>
19
class
LruSet final {
20
public
:
21
explicit
LruSet(size_t max_size,
const
Hash& hash = Hash(),
const
Equal& equal = Equal())
22
: impl_(max_size, hash, equal)
23
{}
24
25
LruSet(LruSet&& lru)
noexcept
=
default
;
26
LruSet(
const
LruSet& lru) =
delete
;
27
LruSet& operator=(LruSet&& lru)
noexcept
=
default
;
28
LruSet& operator=(
const
LruSet& lru) =
delete
;
29
30
/// Adds key to the LRU or updates its usage
31
/// @returns true if key is a new one
32
bool
Put
(
const
T& key) {
return
impl_.Put(key, {}); }
33
34
/// Removes key from LRU
35
void
Erase
(
const
T& key) {
return
impl_.Erase(key); }
36
37
/// Checks whether the key in LRU and updates its usage
38
bool
Has
(
const
T& key) {
return
impl_.Get(key); }
39
40
/// Sets the max size of the LRU, truncates values if new_max_size < GetSize()
41
void
SetMaxSize
(size_t new_max_size) {
return
impl_.SetMaxSize(new_max_size); }
42
43
/// Removes all the elements
44
void
Invalidate
() {
return
impl_.Invalidate(); }
45
46
/// Call Function(const T&) for all items
47
template
<
typename
Function>
48
void
VisitAll
(Function&& func)
const
{
49
impl_.VisitAll([&func](
const
auto
& key,
const
auto
&
/*value*/
)
mutable
{ func(key); });
50
}
51
52
size_t GetSize()
const
{
return
impl_.GetSize(); }
53
54
/// Returns pointer to the least recently used element
55
/// or nullptr if LruSet is empty.
56
/// @warning Returned pointer may be freed on the next set access!
57
const
T*
GetLeastUsed
() {
return
impl_.GetLeastUsedKey(); }
58
59
private
:
60
impl::LruBase<T, impl::EmptyPlaceholder, Hash, Equal> impl_;
61
};
62
63
}
// namespace cache
64
65
USERVER_NAMESPACE_END
userver
cache
lru_set.hpp
Generated on
for userver by
Doxygen
1.17.0