forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRUcache.py
42 lines (35 loc) · 1.07 KB
/
LRUcache.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
# test implementation of LRU cache.
from collections import OrderedDict
class LRUCache(object):
def __init__(self, capacity):
self.__capacity = capacity
self.__cache = OrderedDict()
def set(self, key, value):
try:
self.__cache.pop(key)
except KeyError:
if len(self.__cache) >= self.__capacity:
self.__cache.popitem(last=False)
self.__cache[key] = value
def get(self, key):
try:
value = self.__cache.pop(key)
self.__cache[key] = value
return value
except KeyError:
return -1
def get_cache_details(self):
return self.__cache
if __name__ == "__main__":
cache = LRUCache(2)
cache.set('name', 'john')
cache.set('age', '12')
print cache.get_cache_details()
cache.set('name', 'doey')
print cache.get_cache_details()
print cache.get('age')
print cache.get_cache_details()
cache.set('aaa', 'aaaaa')
print cache.get_cache_details()
print cache.get('age')
print cache.get_cache_details()