SXsid/lru-cache

★ 0Forks 0GoGitHub ↗Compare

README

LRU Cache

A least-recently-used cache with O(1) get and put operations.

The Problem

A cache needs to evict something when full. LRU evicts the item that hasn't been accessed the longest. The challenge is doing both lookup and order tracking in O(1).

Solution: Doubly Linked List + Hashmap

Two constraints need to be satisfied simultaneously:

  • O(1) lookup — only a hashmap gives this
  • O(1) order tracking — need to move any node to head instantly

A doubly linked list lets you detach any node in O(1) because you have both prev and next pointers. A singly linked list can't do this — to detach a node you need its previous node, and finding that is O(n).

The hashmap stores key → pointer to node. The list maintains access order. Together they solve both problems.

Why not alternatives?

  • Array — shifting elements on every access is O(n). Dead on arrival.
  • Singly linked list — detaching a middle node requires O(n) traversal to find the previous node.

Request Flow

Flow

Concurrency

Added sync.Mutex protecting each operation.

Considered a single-threaded event loop (epoll) like Redis, but rejected it — the added complexity doesn't serve the learning goal here. Mutex overhead is acceptable given O(1) operation time.

Contributors

SXsid

Issues