diff --git a/Sprint-2/implement_lru_cache/lru_cache.py b/Sprint-2/implement_lru_cache/lru_cache.py index e69de29..9bdaa6b 100644 --- a/Sprint-2/implement_lru_cache/lru_cache.py +++ b/Sprint-2/implement_lru_cache/lru_cache.py @@ -0,0 +1,30 @@ +from collections import OrderedDict + +class LruCache: + + def __init__(self, limit): + if limit <= 0: + raise ValueError("Limit must be positive") + + 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) \ No newline at end of file