aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/lru-cache.hh
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2023-03-26 21:12:25 -0400
committerJohn Ericson <John.Ericson@Obsidian.Systems>2023-03-31 23:01:40 -0400
commitabd5e7dec039386628223f886b33047734172c8d (patch)
treeca3aecd64e1e9375ab6ed48d4a7dc54ac83b584d /src/libutil/lru-cache.hh
parent8ae9d669409851acb6de39335b11a95a991eae6d (diff)
Extend internal API docs, part 2
Picking up from #8111. Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
Diffstat (limited to 'src/libutil/lru-cache.hh')
-rw-r--r--src/libutil/lru-cache.hh22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/libutil/lru-cache.hh b/src/libutil/lru-cache.hh
index 6ef4a3e06..fe2c677c1 100644
--- a/src/libutil/lru-cache.hh
+++ b/src/libutil/lru-cache.hh
@@ -7,7 +7,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 +33,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 +43,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 +69,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;