0% found this document useful (0 votes)
18 views3 pages

LRU Cache

The document details the internal structure of an LRU (Least Recently Used) Cache, which utilizes a doubly linked list and a hashmap for efficient key-value storage and retrieval. It explains how new key-value pairs are added to the cache and how existing keys are queried, emphasizing the movement of nodes within the linked list to maintain the order of usage. The internal hashmap stores references to nodes, allowing for quick access and updates to the cache entries.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

LRU Cache

The document details the internal structure of an LRU (Least Recently Used) Cache, which utilizes a doubly linked list and a hashmap for efficient key-value storage and retrieval. It explains how new key-value pairs are added to the cache and how existing keys are queried, emphasizing the movement of nodes within the linked list to maintain the order of usage. The internal hashmap stores references to nodes, allowing for quick access and updates to the cache entries.

Uploaded by

Kamal Kannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Internals of LRU Cache

Internal Doubly Linkedlist

Node Node Node Node

Key = k1 Key = k3 Key = k4 Key = k2


Node next Node next Node next Node next
Node prev Node prev Node prev Node prev

Most recently used

Rear Front

Least recently used

Internal Hashmap

Key Value

k1 Node

k2 Node
Storing the nodes of linked list
in the hashmap by their key k3 Node

k4 Node
Add a key-value pair to the Cache

Internal Doubly Linkedlist

Node Node Node Node

Key = k1 Key = k3 Key = k2 Key = k4


Node next Node next Node next Node next
Node prev Node prev Node prev Node prev

Rear Add key (K4) as a new Front


node at the front
LRU Cache

Key Value
Internal Hashmap

k1 Value1 Key Value

k2 k1 Node
Value2

k2 Node
k3 Value3
k3 Node

k4 Node

Add the entry of the new


k4 Value4 node into the Hashmap

Add new key-value pair


Query an existing key from the Cache

LRU Cache Under the hood

Key Value
Internal Doubly Linkedlist
Node Node Node Node
k1 Value1 Key = k1 Key = k3 Key = k4 Key = k2
Node next Node next Node next Node next
Node prev Node prev Node prev Node prev
k2 Value2

k3 Value3
Rear Front

Pluck out the node and


k4 Value4
add it to the front

Node
Get (K4) Node Node Node

Key = k1 Key = k3 Key = k2 Key = k4


Node next Node next Node next Node next
Node prev Node prev Node prev Node prev

Value4

O/P Internal Hashmap

Key Value Key Value

k1 Node k1 Node

k2 Node k2 Node

k3 Node k3 Node

k4 Node k4 Updated Node

Updated the Node for key K4


(update the next and prev pointer)

You might also like