DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
lru_memory_cache.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_LRU_MEMORY_CACHE_H
13 #define DOCWIRE_LRU_MEMORY_CACHE_H
14 
15 #include <functional>
16 #include <limits>
17 #include <list>
18 #include <unordered_map>
19 
20 namespace docwire
21 {
22 
32 template<typename Key, typename Value>
34 {
35 public:
41  explicit lru_memory_cache(size_t max_size = std::numeric_limits<size_t>::max())
42  : m_max_size(max_size)
43  {}
44 
52  Value& get_or_create(const Key& key, const std::function<Value(const Key&)>& producer)
53  {
54  auto it = m_entry_list_iter_map.find(key);
55  if (it != m_entry_list_iter_map.end()) {
56  m_entry_list.splice(m_entry_list.begin(), m_entry_list, it->second);
57  } else {
58  Value value = producer(key);
59  m_entry_list.emplace_front(key, value);
60  m_entry_list_iter_map.emplace(key, m_entry_list.begin());
61  if (m_entry_list.size() > m_max_size) {
62  m_entry_list_iter_map.erase(std::prev(m_entry_list.end())->key);
63  m_entry_list.pop_back();
64  }
65  }
66  return m_entry_list.begin()->value;
67  }
68 
69 private:
70  size_t m_max_size;
71  struct entry
72  {
73  Key key;
74  Value value;
75  entry(const Key& key, const Value& value) : key(key), value(value) {}
76  };
77  std::list<entry> m_entry_list;
78  std::unordered_map<Key, typename std::list<entry>::iterator> m_entry_list_iter_map;
79 };
80 
81 } // namespace docwire
82 
83 #endif // DOCWIRE_LRU_MEMORY_CACHE_H
Least Recently Used (LRU) cache with fixed memory size.
lru_memory_cache(size_t max_size=std::numeric_limits< size_t >::max())
Constructs LRU cache with specified maximum size.
Value & get_or_create(const Key &key, const std::function< Value(const Key &)> &producer)
Returns value for specified key. If key is not in the cache, it calls producer function to create val...
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19