Skip to content

Commit ae9832c

Browse files
committed
Add LRU Cache solution
1 parent 7595e2a commit ae9832c

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed

solutions/object_oriented_design/lru_cache/__init__.py

Whitespace-only changes.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer-primer)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Design an LRU cache"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Constraints and assumptions\n",
22+
"\n",
23+
"* What are we caching?\n",
24+
" * We are cahing the results of web queries\n",
25+
"* Can we assume inputs are valid or do we have to validate them?\n",
26+
" * Assume they're valid\n",
27+
"* Can we assume this fits memory?\n",
28+
" * Yes"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"## Solution"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 1,
41+
"metadata": {
42+
"collapsed": false
43+
},
44+
"outputs": [
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"Overwriting lru_cache.py\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"%%writefile lru_cache.py\n",
55+
"class Node(object):\n",
56+
"\n",
57+
" def __init__(self, results):\n",
58+
" self.results = results\n",
59+
" self.next = next\n",
60+
"\n",
61+
"\n",
62+
"class LinkedList(object):\n",
63+
"\n",
64+
" def __init__(self):\n",
65+
" self.head = None\n",
66+
" self.tail = None\n",
67+
"\n",
68+
" def move_to_front(self, node): # ...\n",
69+
" def append_to_front(self, node): # ...\n",
70+
" def remove_from_tail(self): # ...\n",
71+
"\n",
72+
"\n",
73+
"class Cache(object):\n",
74+
"\n",
75+
" def __init__(self, MAX_SIZE):\n",
76+
" self.MAX_SIZE = MAX_SIZE\n",
77+
" self.size = 0\n",
78+
" self.lookup = {} # key: query, value: node\n",
79+
" self.linked_list = LinkedList()\n",
80+
"\n",
81+
" def get(self, query)\n",
82+
" \"\"\"Get the stored query result from the cache.\n",
83+
" \n",
84+
" Accessing a node updates its position to the front of the LRU list.\n",
85+
" \"\"\"\n",
86+
" node = self.lookup[query]\n",
87+
" if node is None:\n",
88+
" return None\n",
89+
" self.linked_list.move_to_front(node)\n",
90+
" return node.results\n",
91+
"\n",
92+
" def set(self, results, query):\n",
93+
" \"\"\"Set the result for the given query key in the cache.\n",
94+
" \n",
95+
" When updating an entry, updates its position to the front of the LRU list.\n",
96+
" If the entry is new and the cache is at capacity, removes the oldest entry\n",
97+
" before the new entry is added.\n",
98+
" \"\"\"\n",
99+
" node = self.lookup[query]\n",
100+
" if node is not None:\n",
101+
" # Key exists in cache, update the value\n",
102+
" node.results = results\n",
103+
" self.linked_list.move_to_front(node)\n",
104+
" else:\n",
105+
" # Key does not exist in cache\n",
106+
" if self.size == self.MAX_SIZE:\n",
107+
" # Remove the oldest entry from the linked list and lookup\n",
108+
" self.lookup.pop(self.linked_list.tail.query, None)\n",
109+
" self.linked_list.remove_from_tail()\n",
110+
" else:\n",
111+
" self.size += 1\n",
112+
" # Add the new key and value\n",
113+
" new_node = Node(results)\n",
114+
" self.linked_list.append_to_front(new_node)\n",
115+
" self.lookup[query] = new_node"
116+
]
117+
}
118+
],
119+
"metadata": {
120+
"kernelspec": {
121+
"display_name": "Python 3",
122+
"language": "python",
123+
"name": "python3"
124+
},
125+
"language_info": {
126+
"codemirror_mode": {
127+
"name": "ipython",
128+
"version": 3
129+
},
130+
"file_extension": ".py",
131+
"mimetype": "text/x-python",
132+
"name": "python",
133+
"nbconvert_exporter": "python",
134+
"pygments_lexer": "ipython3",
135+
"version": "3.4.3"
136+
}
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 0
140+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Node(object):
2+
3+
def __init__(self, results):
4+
self.results = results
5+
self.next = next
6+
7+
8+
class LinkedList(object):
9+
10+
def __init__(self):
11+
self.head = None
12+
self.tail = None
13+
14+
def move_to_front(self, node): # ...
15+
def append_to_front(self, node): # ...
16+
def remove_from_tail(self): # ...
17+
18+
19+
class Cache(object):
20+
21+
def __init__(self, MAX_SIZE):
22+
self.MAX_SIZE = MAX_SIZE
23+
self.size = 0
24+
self.lookup = {} # key: query, value: node
25+
self.linked_list = LinkedList()
26+
27+
def get(self, query)
28+
"""Get the stored query result from the cache.
29+
30+
Accessing a node updates its position to the front of the LRU list.
31+
"""
32+
node = self.lookup[query]
33+
if node is None:
34+
return None
35+
self.linked_list.move_to_front(node)
36+
return node.results
37+
38+
def set(self, results, query):
39+
"""Set the result for the given query key in the cache.
40+
41+
When updating an entry, updates its position to the front of the LRU list.
42+
If the entry is new and the cache is at capacity, removes the oldest entry
43+
before the new entry is added.
44+
"""
45+
node = self.lookup[query]
46+
if node is not None:
47+
# Key exists in cache, update the value
48+
node.results = results
49+
self.linked_list.move_to_front(node)
50+
else:
51+
# Key does not exist in cache
52+
if self.size == self.MAX_SIZE:
53+
# Remove the oldest entry from the linked list and lookup
54+
self.lookup.pop(self.linked_list.tail.query, None)
55+
self.linked_list.remove_from_tail()
56+
else:
57+
self.size += 1
58+
# Add the new key and value
59+
new_node = Node(results)
60+
self.linked_list.append_to_front(new_node)
61+
self.lookup[query] = new_node

0 commit comments

Comments
 (0)