-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-lru_cache.py
44 lines (33 loc) · 1.02 KB
/
3-lru_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python3
'''LRU Caching
'''
from base_caching import BaseCaching
class LRUCache(BaseCaching):
'''Represents least recently used caching object
inherited from BaseCaching class
'''
def __init__(self):
'''Intilizes the oject
'''
super().__init__()
self.queue = []
def put(self, key, item):
'''Inserts an item in to the cach_date usding
the LRU technique
'''
if key is None or item is None:
return
if len(self.cache_data) >= self.MAX_ITEMS:
tobe_discard = self.queue.pop(0)
print('DISCARD: {}'.format(tobe_discard))
del self.cache_data[tobe_discard]
self.queue.append(key)
self.cache_data[key] = item
def get(self, key):
'''Returns the cache data which are linked to the key
'''
if key is None or key not in self.cache_data:
return None
self.queue.remove(key)
self.queue.append(key)
return self.cache_data[key]