From 8f3326c8d4e058d44c0af4beaa965ce048971b8b Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 4 Sep 2022 16:31:46 +0300 Subject: [PATCH] Create LRU helper class for caching --- src/utils/helpers.py | 20 +++++++++++++++++++- tests/test_helpers.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/utils/helpers.py b/src/utils/helpers.py index 52ebc330..7ea6580c 100644 --- a/src/utils/helpers.py +++ b/src/utils/helpers.py @@ -2,6 +2,7 @@ import pathlib import sys from os.path import join +from collections import OrderedDict def is_none_or_empty(item): @@ -60,4 +61,21 @@ def load_model(model_name, model_dir, model_type, device:str=None): def is_pyinstaller_app(): "Returns true if the app is running from Native GUI created by PyInstaller" - return getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS') \ No newline at end of file + return getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS') + + +class LRU(OrderedDict): + def __init__(self, *args, capacity=128, **kwargs): + self.capacity = capacity + super().__init__(*args, **kwargs) + + def __getitem__(self, key): + value = super().__getitem__(key) + self.move_to_end(key) + return value + + def __setitem__(self, key, value): + super().__setitem__(key, value) + if len(self) > self.capacity: + oldest = next(iter(self)) + del self[oldest] diff --git a/tests/test_helpers.py b/tests/test_helpers.py index d4f06e6d..c9b1cd75 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -28,3 +28,18 @@ def test_merge_dicts(): # do not override existing key in priority_dict with default dict assert helpers.merge_dicts(priority_dict={'a': 1}, default_dict={'a': 2}) == {'a': 1} + + +def test_lru_cache(): + # Test initializing cache + cache = helpers.LRU({'a': 1, 'b': 2}, capacity=2) + assert cache == {'a': 1, 'b': 2} + + # Test capacity overflow + cache['c'] = 3 + assert cache == {'b': 2, 'c': 3} + + # Test delete least recently used item from LRU cache on capacity overflow + cache['b'] # accessing 'b' makes it the most recently used item + cache['d'] = 4 # so 'c' is deleted from the cache instead of 'b' + assert cache == {'b': 2, 'd': 4}