|
| 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 | +} |
0 commit comments