class LRUCache(FIFOCache): '''Least recently used object cache. Args: max_items: The maximum number of items to keep time_to_live: The time in seconds of how long to keep the item ''' def __init__(self, max_items=None, time_to_live=None): super().__init__(max_items=max_items, time_to_live=time_to_live) self._seq = LinkedList() def __getitem__(self, key): self.trim() self.touch(key) return self._map[key].value def __setitem__(self, key, value): if key in self._map: self._map[key].value = value self.touch(key) else: item = CacheItem(key, value, self._time_to_live) self._map[key] = item self._seq.append(item) self.trim() def touch(self, key): self._map[key].access_time = time.time() self._seq.remove(self._map[key]) self._seq.append(self._map[key])
def test_linked_list(self): linked_list = LinkedList() for dummy in range(2): self.assertEqual(0, len(linked_list)) linked_list.append('a') self.assertEqual(1, len(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.append('b') self.assertEqual(2, len(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('b', linked_list.tail.value) linked_list.append('c') self.assertEqual(3, len(linked_list)) self.assertEqual(('a', 'b', 'c'), tuple(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) self.assertEqual('a', linked_list[0]) self.assertEqual('b', linked_list[1]) self.assertEqual('c', linked_list[2]) linked_list.appendleft('d') self.assertEqual(4, len(linked_list)) self.assertEqual(('d', 'a', 'b', 'c'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('a') self.assertEqual(3, len(linked_list)) self.assertEqual(('d', 'b', 'c'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.append('a') self.assertEqual(4, len(linked_list)) self.assertEqual(('d', 'b', 'c', 'a'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.remove('d') self.assertEqual(3, len(linked_list)) self.assertEqual(('b', 'c', 'a'), tuple(linked_list)) self.assertEqual('b', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.remove('a') self.assertEqual(2, len(linked_list)) self.assertEqual(('b', 'c'), tuple(linked_list)) self.assertEqual('b', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('b') self.assertEqual(1, len(linked_list)) self.assertEqual(('c', ), tuple(linked_list)) self.assertEqual('c', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('c') self.assertEqual(0, len(linked_list)) self.assertEqual((), tuple(linked_list)) self.assertRaises(ValueError, linked_list.remove, 'asdf') linked_list.appendleft('a') linked_list.append('b') linked_list.appendleft('c') linked_list.append('d') self.assertEqual(4, len(linked_list)) self.assertEqual(('c', 'a', 'b', 'd'), tuple(linked_list)) self.assertEqual('c', linked_list.popleft()) self.assertEqual('d', linked_list.pop()) self.assertEqual(2, len(linked_list)) self.assertEqual(('a', 'b'), tuple(linked_list)) linked_list.clear()
def test_linked_list(self): linked_list = LinkedList() for dummy in range(2): self.assertEqual(0, len(linked_list)) linked_list.append('a') self.assertEqual(1, len(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.append('b') self.assertEqual(2, len(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('b', linked_list.tail.value) linked_list.append('c') self.assertEqual(3, len(linked_list)) self.assertEqual(('a', 'b', 'c'), tuple(linked_list)) self.assertEqual('a', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) self.assertEqual('a', linked_list[0]) self.assertEqual('b', linked_list[1]) self.assertEqual('c', linked_list[2]) linked_list.appendleft('d') self.assertEqual(4, len(linked_list)) self.assertEqual(('d', 'a', 'b', 'c'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('a') self.assertEqual(3, len(linked_list)) self.assertEqual(('d', 'b', 'c'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.append('a') self.assertEqual(4, len(linked_list)) self.assertEqual(('d', 'b', 'c', 'a'), tuple(linked_list)) self.assertEqual('d', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.remove('d') self.assertEqual(3, len(linked_list)) self.assertEqual(('b', 'c', 'a'), tuple(linked_list)) self.assertEqual('b', linked_list.head.value) self.assertEqual('a', linked_list.tail.value) linked_list.remove('a') self.assertEqual(2, len(linked_list)) self.assertEqual(('b', 'c'), tuple(linked_list)) self.assertEqual('b', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('b') self.assertEqual(1, len(linked_list)) self.assertEqual(('c',), tuple(linked_list)) self.assertEqual('c', linked_list.head.value) self.assertEqual('c', linked_list.tail.value) linked_list.remove('c') self.assertEqual(0, len(linked_list)) self.assertEqual((), tuple(linked_list)) self.assertRaises(ValueError, linked_list.remove, 'asdf') linked_list.appendleft('a') linked_list.append('b') linked_list.appendleft('c') linked_list.append('d') self.assertEqual(4, len(linked_list)) self.assertEqual(('c', 'a', 'b', 'd'), tuple(linked_list)) self.assertEqual('c', linked_list.popleft()) self.assertEqual('d', linked_list.pop()) self.assertEqual(2, len(linked_list)) self.assertEqual(('a', 'b'), tuple(linked_list)) linked_list.clear()
def __init__(self, max_items=None, time_to_live=None): super().__init__(max_items=max_items, time_to_live=time_to_live) self._seq = LinkedList()