aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/lru-cache.hh
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2023-04-02 16:21:38 -0400
committerJohn Ericson <John.Ericson@Obsidian.Systems>2023-04-02 16:21:38 -0400
commitb2c9315bf2ddda4d4c5ea5a49471114553ddff87 (patch)
tree8ec57641ed5253da2bd4f18d70bead6b080f8505 /src/libutil/lru-cache.hh
parentd1d1ae7a3b97059af09dd5a5dde2e37ada0fddac (diff)
parent2ef99cd10489929a755831251c3fad8f3df2faeb (diff)
Merge remote-tracking branch 'upstream/master' into list-experimental-features
Diffstat (limited to 'src/libutil/lru-cache.hh')
-rw-r--r--src/libutil/lru-cache.hh23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/libutil/lru-cache.hh b/src/libutil/lru-cache.hh
index 6ef4a3e06..0e19517ed 100644
--- a/src/libutil/lru-cache.hh
+++ b/src/libutil/lru-cache.hh
@@ -1,4 +1,5 @@
#pragma once
+///@file
#include <cassert>
#include <map>
@@ -7,7 +8,9 @@
namespace nix {
-/* A simple least-recently used cache. Not thread-safe. */
+/**
+ * A simple least-recently used cache. Not thread-safe.
+ */
template<typename Key, typename Value>
class LRUCache
{
@@ -31,7 +34,9 @@ public:
LRUCache(size_t capacity) : capacity(capacity) { }
- /* Insert or upsert an item in the cache. */
+ /**
+ * Insert or upsert an item in the cache.
+ */
void upsert(const Key & key, const Value & value)
{
if (capacity == 0) return;
@@ -39,7 +44,9 @@ public:
erase(key);
if (data.size() >= capacity) {
- /* Retire the oldest item. */
+ /**
+ * Retire the oldest item.
+ */
auto oldest = lru.begin();
data.erase(*oldest);
lru.erase(oldest);
@@ -63,14 +70,18 @@ public:
return true;
}
- /* Look up an item in the cache. If it exists, it becomes the most
- recently used item. */
+ /**
+ * Look up an item in the cache. If it exists, it becomes the most
+ * recently used item.
+ * */
std::optional<Value> get(const Key & key)
{
auto i = data.find(key);
if (i == data.end()) return {};
- /* Move this item to the back of the LRU list. */
+ /**
+ * Move this item to the back of the LRU list.
+ */
lru.erase(i->second.first.it);
auto j = lru.insert(lru.end(), i);
i->second.first.it = j;