Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

unordered_lru_cache.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef DASH_UNORDERED_LRU_CACHE_H
6 #define DASH_UNORDERED_LRU_CACHE_H
7 
8 #include <unordered_map>
9 
10 template<typename Key, typename Value, typename Hasher, size_t MaxSize = 0, size_t TruncateThreshold = 0>
12 {
13 private:
14  typedef std::unordered_map<Key, std::pair<Value, int64_t>, Hasher> MapType;
15 
17  size_t maxSize;
19  int64_t accessCounter{0};
20 
21 public:
22  explicit unordered_lru_cache(size_t _maxSize = MaxSize, size_t _truncateThreshold = TruncateThreshold) :
23  maxSize(_maxSize),
24  truncateThreshold(_truncateThreshold == 0 ? _maxSize * 2 : _truncateThreshold)
25  {
26  // either specify maxSize through template arguments or the contructor and fail otherwise
27  assert(_maxSize != 0);
28  }
29 
30 
31  template<typename Value2>
32  void _emplace(const Key& key, Value2&& v)
33  {
35  auto it = cacheMap.find(key);
36  if (it == cacheMap.end()) {
37  cacheMap.emplace(key, std::make_pair(std::forward<Value2>(v), accessCounter++));
38  } else {
39  it->second.first = std::forward<Value2>(v);
40  it->second.second = accessCounter++;
41  }
42  }
43 
44  void emplace(const Key& key, Value&& v)
45  {
46  _emplace(key, v);
47  }
48 
49  void insert(const Key& key, const Value& v)
50  {
51  _emplace(key, v);
52  }
53 
54  bool get(const Key& key, Value& value)
55  {
56  auto it = cacheMap.find(key);
57  if (it != cacheMap.end()) {
58  it->second.second = accessCounter++;
59  value = it->second.first;
60  return true;
61  }
62  return false;
63  }
64 
65  bool exists(const Key& key)
66  {
67  auto it = cacheMap.find(key);
68  if (it != cacheMap.end()) {
69  it->second.second = accessCounter++;
70  return true;
71  }
72  return false;
73  }
74 
75  void erase(const Key& key)
76  {
77  cacheMap.erase(key);
78  }
79 
80  void clear()
81  {
82  cacheMap.clear();
83  }
84 
85 private:
87  {
88  typedef typename MapType::iterator Iterator;
89 
90  if (cacheMap.size() <= truncateThreshold) {
91  return;
92  }
93 
94  std::vector<Iterator> vec;
95  vec.reserve(cacheMap.size());
96  for (auto it = cacheMap.begin(); it != cacheMap.end(); ++it) {
97  vec.emplace_back(it);
98  }
99  // sort by last access time (descending order)
100  std::sort(vec.begin(), vec.end(), [](const Iterator& it1, const Iterator& it2) {
101  return it1->second.second > it2->second.second;
102  });
103 
104  for (size_t i = maxSize; i < vec.size(); i++) {
105  cacheMap.erase(vec[i]);
106  }
107  }
108 };
109 
110 #endif // DASH_UNORDERED_LRU_CACHE_H
void erase(const Key &key)
unordered_lru_cache(size_t _maxSize=MaxSize, size_t _truncateThreshold=TruncateThreshold)
void insert(const Key &key, const Value &v)
bool exists(const Key &key)
void _emplace(const Key &key, Value2 &&v)
std::unordered_map< Key, std::pair< Value, int64_t >, Hasher > MapType
void emplace(const Key &key, Value &&v)
Released under the MIT license