Least Recently Used Cache
agentultra.com·2h
🔗Weak References
Preview
Report Post

Today we’re going to implement a Least Recently Used Cache in C++23.

This structure is basically a fixed-size mapping of keys to values where the least recently used, or oldest, keys are evicted from the cache when adding new keys.

Fetching a value from the cache marks the key as most recently used as do successful insert operations.

First, we’ll add some imports and write a small program to demonstrate our cache:

#include <cstdint>
#include <list>
#include <map>
#include <optional>
#include <print>

int main() {
LRU<std::string, uint64_t> lru(3);
lru.set("foo", 23);
lru.set("bar", 44);
lru.set("baz", 12);

std::print("lru capacity: {}\n", lru.capacity());
std::print("lru size: {}\n", lru.size());

std::optional<uint64_t> foo = lru.get("foo");
std::print("foo: {}\n", ...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help