ModErn Text Analysis
META Enumerates Textual Applications
dblru_cache.h
Go to the documentation of this file.
1 
10 #ifndef META_DBLRU_CACHE_H_
11 #define META_DBLRU_CACHE_H_
12 
13 #if META_HAS_STD_SHARED_PTR_ATOMICS
14 #include <atomic>
15 #else
16 #include <mutex>
17 #include "util/shim.h"
18 #endif
19 #include <functional>
20 #include <vector>
21 
23 #include "util/optional.h"
24 
25 namespace meta
26 {
27 namespace caching
28 {
29 
48 template<class Key, class Value,
49  template <class, class> class Map = locking_map>
51 {
52  public:
57  dblru_cache(uint64_t max_size);
58 
63 
69 
73  ~dblru_cache() = default;
74 
79  void swap(dblru_cache& other);
80 
86  void insert(const Key& key, const Value& value);
87 
95  template <class... Args>
96  void emplace(Args&&... args);
97 
105  util::optional<Value> find(const Key& key);
106 
108  void clear();
109 
110  private:
115  void handle_insert();
116 
120  std::shared_ptr<Map<Key, Value>> get_primary_map() const;
121 
125  std::shared_ptr<Map<Key, Value>> get_secondary_map() const;
126 
130  uint64_t max_size_;
131 
135 #if META_HAS_STD_SHARED_PTR_ATOMICS
136  std::atomic<uint64_t> current_size_;
138 #else
139  uint64_t current_size_;
140  std::unique_ptr<std::mutex> mutables_{make_unique<std::mutex>()};
141 #endif
142 
146  std::shared_ptr<Map<Key, Value>> primary_;
147 
151  std::shared_ptr<Map<Key, Value>> secondary_;
152 };
153 
157 template <class Key, class Value>
159 }
160 }
161 
162 #include "caching/dblru_cache.tcc"
163 #endif
std::shared_ptr< Map< Key, Value > > get_primary_map() const
Gets the primary map.
Definition: dblru_cache.tcc:76
std::shared_ptr< Map< Key, Value > > get_secondary_map() const
Gets the secondary map.
Definition: dblru_cache.tcc:88
dblru_cache & operator=(dblru_cache)
dlbru_cache may be be assigned.
Definition: dblru_cache.tcc:46
std::shared_ptr< Map< Key, Value > > secondary_
The secondary map.
Definition: dblru_cache.h:151
A double-barrel approach at a LRU cache.
Definition: dblru_cache.h:50
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
dblru_cache(uint64_t max_size)
Constructs a dlbru_cache with a given fixed size.
Definition: dblru_cache.tcc:13
A simple wrapper around a std::unordered_map that uses an internal mutex for synchronization safety...
Definition: locking_map.h:28
uint64_t max_size_
The maximum allowed size for the cache.
Definition: dblru_cache.h:130
std::shared_ptr< Map< Key, Value > > primary_
The primary map.
Definition: dblru_cache.h:146
void emplace(Args &&...args)
Inserts a key value pair into the cache using in-place construction if possible.
Definition: dblru_cache.tcc:108
uint64_t current_size_
The current size of the primary map.
Definition: dblru_cache.h:139
~dblru_cache()=default
Default destructor.
void insert(const Key &key, const Value &value)
Insert a given (key, value) pair into the cache.
Definition: dblru_cache.tcc:99
void clear()
Empties the cache.
Definition: dblru_cache.tcc:169
void handle_insert()
Helper function to ensure that the primary and secondary map swapping occurs at the correct moment...
Definition: dblru_cache.tcc:149
util::optional< Value > find(const Key &key)
Finds a value in the cache.
Definition: dblru_cache.tcc:116
void swap(dblru_cache &other)
Swaps the current dlbru_cache with a given one.
Definition: dblru_cache.tcc:63