|
9 | 9 |
|
10 | 10 | According to functions, all storage types are not necessarily available. |
11 | 11 |
|
| 12 | +Readings uses a LRU cache system with a TTL. It's possible to configure it with environment variables : |
| 13 | +- 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. |
| 14 | +- ROK4_READING_LRU_CACHE_TTL : Validity duration of cached element, in seconds. Default 300. 0 or negative integer to disable time validity. |
| 15 | +
|
| 16 | +To disable cache, set ROK4_READING_LRU_CACHE_SIZE to 1 and ROK4_READING_LRU_CACHE_TTL to 0. |
| 17 | +
|
12 | 18 | Using CEPH storage requires environment variables : |
13 | 19 | - ROK4_CEPH_CONFFILE |
14 | 20 | - ROK4_CEPH_USERNAME |
|
69 | 75 | __S3_CLIENTS = {} |
70 | 76 | __S3_DEFAULT_CLIENT = None |
71 | 77 |
|
| 78 | +__LRU_SIZE = 64 |
| 79 | +__LRU_TTL = 300 |
| 80 | + |
| 81 | +try: |
| 82 | + __LRU_SIZE = int(os.environ["ROK4_READING_LRU_CACHE_SIZE"]) |
| 83 | + if __LRU_SIZE < 1: |
| 84 | + __LRU_SIZE = None |
| 85 | +except ValueError: |
| 86 | + pass |
| 87 | +except KeyError: |
| 88 | + pass |
| 89 | + |
| 90 | +try: |
| 91 | + __LRU_TTL = int(os.environ["ROK4_READING_LRU_CACHE_TTL"]) |
| 92 | + if __LRU_TTL < 0: |
| 93 | + __LRU_TTL = 0 |
| 94 | +except ValueError: |
| 95 | + pass |
| 96 | +except KeyError: |
| 97 | + pass |
| 98 | + |
72 | 99 |
|
73 | 100 | def __get_ttl_hash(): |
74 | | - """Return the same value withing 5 minutes time period""" |
75 | | - return round(time.time() / 300) |
| 101 | + """Return the time string rounded according to time-to-live value""" |
| 102 | + if __LRU_TTL == 0: |
| 103 | + return time.time() |
| 104 | + else: |
| 105 | + return round(time.time() / __LRU_TTL) |
76 | 106 |
|
77 | 107 |
|
78 | 108 | def __get_s3_client(bucket_name: str) -> Tuple[Dict[str, Union["boto3.client", str]], str, str]: |
@@ -294,7 +324,7 @@ def get_data_str(path: str) -> str: |
294 | 324 | return get_data_binary(path).decode("utf-8") |
295 | 325 |
|
296 | 326 |
|
297 | | -@lru_cache(maxsize=50) |
| 327 | +@lru_cache(maxsize=__LRU_SIZE) |
298 | 328 | def __get_cached_data_binary(path: str, ttl_hash: int, range: Tuple[int, int] = None) -> str: |
299 | 329 | """Load data into a binary string, using a LRU cache |
300 | 330 |
|
|
0 commit comments