34#include <unordered_map>
43template <
typename Key,
typename Value>
class LRUCache
46 using Clock = std::chrono::steady_clock;
72 std::optional<Value>
get(
const Key& key)
74 std::lock_guard<std::mutex> lock(mutex_);
76 auto it = cacheMap_.find(key);
77 if (it == cacheMap_.end())
86 auto age = std::chrono::duration_cast<std::chrono::seconds>(Clock::now() -
87 it->second->timestamp)
89 if (age > ttlSeconds_)
92 cacheList_.erase(it->second);
101 cacheList_.splice(cacheList_.begin(), cacheList_, it->second);
104 return it->second->value;
115 void put(
const Key& key,
const Value& value)
117 std::lock_guard<std::mutex> lock(mutex_);
119 auto it = cacheMap_.find(key);
120 if (it != cacheMap_.end())
123 it->second->value = value;
124 it->second->timestamp = Clock::now();
125 cacheList_.splice(cacheList_.begin(), cacheList_, it->second);
130 while (cacheList_.size() >= maxSize_)
132 auto last = cacheList_.back();
133 cacheMap_.erase(last.key);
134 cacheList_.pop_back();
139 cacheList_.emplace_front(CacheEntry{key, value, Clock::now()});
140 cacheMap_[key] = cacheList_.begin();
146 void put(
const Key& key, Value&& value)
148 std::lock_guard<std::mutex> lock(mutex_);
150 auto it = cacheMap_.find(key);
151 if (it != cacheMap_.end())
153 it->second->value = std::move(value);
154 it->second->timestamp = Clock::now();
155 cacheList_.splice(cacheList_.begin(), cacheList_, it->second);
159 while (cacheList_.size() >= maxSize_)
161 auto last = cacheList_.back();
162 cacheMap_.erase(last.key);
163 cacheList_.pop_back();
167 cacheList_.emplace_front(CacheEntry{key, std::move(value), Clock::now()});
168 cacheMap_[key] = cacheList_.begin();
176 std::lock_guard<std::mutex> lock(mutex_);
177 auto it = cacheMap_.find(key);
178 if (it == cacheMap_.end())
182 auto age = std::chrono::duration_cast<std::chrono::seconds>(Clock::now() -
183 it->second->timestamp)
185 if (age > ttlSeconds_)
197 std::lock_guard<std::mutex> lock(mutex_);
199 auto it = cacheMap_.find(key);
200 if (it == cacheMap_.end())
203 cacheList_.erase(it->second);
213 std::lock_guard<std::mutex> lock(mutex_);
223 std::lock_guard<std::mutex> lock(mutex_);
224 return cacheList_.size();
240 std::lock_guard<std::mutex> lock(mutex_);
241 return cacheList_.empty();
260 return total > 0 ?
static_cast<float>(
hits) /
static_cast<float>(total) : 0.0f;
269 std::lock_guard<std::mutex> lock(mutex_);
270 return Stats{hits_, misses_, evictions_, expirations_, cacheList_.size(), maxSize_};
278 std::lock_guard<std::mutex> lock(mutex_);
279 hits_ = misses_ = evictions_ = expirations_ = 0;
288 if (ttlSeconds_ <= 0)
291 std::lock_guard<std::mutex> lock(mutex_);
293 auto now = Clock::now();
295 auto it = cacheList_.begin();
296 while (it != cacheList_.end())
299 std::chrono::duration_cast<std::chrono::seconds>(now - it->timestamp).count();
300 if (age > ttlSeconds_)
302 cacheMap_.erase(it->key);
303 it = cacheList_.erase(it);
324 using CacheList = std::list<CacheEntry>;
325 using CacheMap = std::unordered_map<Key, typename CacheList::iterator>;
330 mutable std::mutex mutex_;
331 CacheList cacheList_;
337 size_t evictions_ = 0;
338 size_t expirations_ = 0;
LRUCache & operator=(LRUCache &&)=delete
bool contains(const Key &key) const
Check if key exists and is not expired (without updating LRU order).
LRUCache(LRUCache &&)=delete
LRUCache(size_t maxSize, int ttlSeconds=0)
Construct an LRU cache.
bool remove(const Key &key)
Remove a specific key from cache.
void put(const Key &key, const Value &value)
Put a value in the cache.
std::chrono::steady_clock Clock
void clear()
Clear all entries.
LRUCache & operator=(const LRUCache &)=delete
std::optional< Value > get(const Key &key)
Get a value from the cache.
size_t pruneExpired()
Remove expired entries (useful for periodic cleanup).
size_t size() const
Get current number of entries.
Stats getStats() const
Get cache statistics.
size_t maxSize() const
Get maximum capacity.
LRUCache(const LRUCache &)=delete
void put(const Key &key, Value &&value)
Put a value using move semantics.
Clock::time_point TimePoint
void resetStats()
Reset statistics counters.
bool empty() const
Check if cache is empty.