Skip to content

Commit

Permalink
fixup! Introduce a simple template LRU Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
maneeshpm committed Dec 14, 2021
1 parent 06eadb2 commit 54f44ff
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/tools/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <list>
#include <unordered_map>
#include <assert.h>
#include <mutex>

template <typename Key_t, typename Value_t>
class LRUCache {
Expand All @@ -36,6 +37,7 @@ class LRUCache {
}

Value_t get(const Key_t& k) {
std::lock_guard<std::mutex> locker(m_mutex);
auto it = m_map.find(k);
if (it == m_map.end()) {
throw std::runtime_error("Entry not cached.");
Expand All @@ -46,6 +48,7 @@ class LRUCache {
}

void put(const Key_t& k, const Value_t& v) {
std::lock_guard<std::mutex> locker(m_mutex);
auto it = m_map.find(k);
if (it != m_map.end()) {
m_list.erase(it->second);
Expand All @@ -69,4 +72,5 @@ class LRUCache {
std::unordered_map<Key_t, list_iterator_t> m_map;
std::list<Key_Value_pair_t> m_list;
unsigned int m_size;
std::mutex m_mutex;
};

0 comments on commit 54f44ff

Please sign in to comment.