From 60606ccfc431de2e5f68698b214a3bee783d758e Mon Sep 17 00:00:00 2001 From: Faithy4444 <161722786+Faithy4444@users.noreply.github.com> Date: Fri, 24 Oct 2025 23:24:09 +0200 Subject: [PATCH] lru cache redone with correct upstream branch --- Sprint-2/implement_lru_cache/lru_cache.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..6c2258b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,22 @@ +from collections import OrderedDict + +class LruCache: + def __init__(self, limit): + if limit <= 0: + raise ValueError("Cache limit must be greater than zero.") + self.limit = limit + self.cache = OrderedDict() + + def get(self, key): + if key not in self.cache: + return None + self.cache.move_to_end(key) + return self.cache[key] + + def set(self, key, value): + if key in self.cache: + self.cache.move_to_end(key) + self.cache[key] = value + + if len(self.cache) > self.limit: + self.cache.popitem(last=False)