We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 134e683 commit 60606ccCopy full SHA for 60606cc
Sprint-2/implement_lru_cache/lru_cache.py
@@ -0,0 +1,22 @@
1
+from collections import OrderedDict
2
+
3
+class LruCache:
4
+ def __init__(self, limit):
5
+ if limit <= 0:
6
+ raise ValueError("Cache limit must be greater than zero.")
7
+ self.limit = limit
8
+ self.cache = OrderedDict()
9
10
+ def get(self, key):
11
+ if key not in self.cache:
12
+ return None
13
+ self.cache.move_to_end(key)
14
+ return self.cache[key]
15
16
+ def set(self, key, value):
17
+ if key in self.cache:
18
19
+ self.cache[key] = value
20
21
+ if len(self.cache) > self.limit:
22
+ self.cache.popitem(last=False)
0 commit comments