Skip to content

Commit 5a81599

Browse files
committed
feat: implement LRU cache set method
1 parent 22ff7c7 commit 5a81599

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎Sprint-2/implement_lru_cache/lru_cache.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,19 @@ def get(self,key):
5858
self.move_to_head(target)
5959
return target.value
6060

61+
def set(self,key,value):
62+
target = self.cache.get(key)
63+
64+
if target is not None:
65+
target.value = value
66+
self.move_to_head(target)
67+
68+
else:
69+
target = self.clsNode(value,key)
70+
self.add_to_head(target)
71+
self.cache[key] = target
72+
73+
if len(self.cache) > self.limit:
74+
old_tail = self.tail
75+
self.remove(old_tail)
76+
del self.cache[old_tail.key]

0 commit comments

Comments
 (0)