From ac5576c00cd079a53ba5b30ca8a97934c5019740 Mon Sep 17 00:00:00 2001 From: Theo Satabin Date: Mon, 9 Oct 2023 13:30:12 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Configurabilit=C3=A9=20du=20cache=20de=20le?= =?UTF-8?q?cture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### [Added] * `storage` : le cache de lecture est configurable en taille (avec ROK4_READING_LRU_CACHE_SIZE) et en temps de rétention (avec ROK4_READING_LRU_CACHE_TTL) --- src/rok4/storage.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/rok4/storage.py b/src/rok4/storage.py index fc7a775..d0b9c70 100644 --- a/src/rok4/storage.py +++ b/src/rok4/storage.py @@ -9,6 +9,12 @@ According to functions, all storage types are not necessarily available. +Readings uses a LRU cache system with a TTL. It's possible to configure it with environment variables : +- ROK4_READING_LRU_CACHE_SIZE : Number of cached element. Default 64. Set 0 or a negative integer to configure a cache without bound. A power of two make cache more efficient. +- ROK4_READING_LRU_CACHE_TTL : Validity duration of cached element, in seconds. Default 300. 0 or negative integer to disable time validity. + +To disable cache, set ROK4_READING_LRU_CACHE_SIZE to 1 and ROK4_READING_LRU_CACHE_TTL to 0. + Using CEPH storage requires environment variables : - ROK4_CEPH_CONFFILE - ROK4_CEPH_USERNAME @@ -69,10 +75,33 @@ __S3_CLIENTS = {} __S3_DEFAULT_CLIENT = None +__LRU_SIZE = 64 +__LRU_TTL = 300 + +try: + __LRU_SIZE = int(os.environ["ROK4_READING_LRU_CACHE_SIZE"]) + if __LRU_SIZE < 1: + __LRU_SIZE = None +except ValueError: + pass +except KeyError: + pass + +try: + __LRU_TTL = int(os.environ["ROK4_READING_LRU_CACHE_TTL"]) + if __LRU_TTL < 0: + __LRU_TTL = 0 +except ValueError: + pass +except KeyError: + pass def __get_ttl_hash(): - """Return the same value withing 5 minutes time period""" - return round(time.time() / 300) + """Return the time string rounded according to time-to-live value""" + if __LRU_TTL == 0: + return time.time() + else: + return round(time.time() / __LRU_TTL) def __get_s3_client(bucket_name: str) -> Tuple[Dict[str, Union["boto3.client", str]], str, str]: @@ -294,7 +323,7 @@ def get_data_str(path: str) -> str: return get_data_binary(path).decode("utf-8") -@lru_cache(maxsize=50) +@lru_cache(maxsize=__LRU_SIZE) def __get_cached_data_binary(path: str, ttl_hash: int, range: Tuple[int, int] = None) -> str: """Load data into a binary string, using a LRU cache From 8a5dc1b17ddc2678c4f82464727a900cf9acffe1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:26:41 +0000 Subject: [PATCH 2/2] =?UTF-8?q?[pre-commit.ci]=20Corrections=20automatique?= =?UTF-8?q?s=20appliqu=C3=A9es=20par=20les=20git=20hooks.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rok4/storage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rok4/storage.py b/src/rok4/storage.py index d0b9c70..a3c6975 100644 --- a/src/rok4/storage.py +++ b/src/rok4/storage.py @@ -96,6 +96,7 @@ except KeyError: pass + def __get_ttl_hash(): """Return the time string rounded according to time-to-live value""" if __LRU_TTL == 0: