rediscache 0.3.3__tar.gz → 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: rediscache
3
- Version: 0.3.3
3
+ Version: 1.0.0
4
4
  Summary: Redis caching of functions evolving over time
5
5
  Home-page: https://github.com/AmadeusITGroup/RedisCache
6
6
  License: MIT
@@ -115,22 +115,6 @@ See `test_rediscache.py` for more examples.
115
115
 
116
116
  Note: when you choose to wait for the value, you do not have an absolute guarantee that you will not get the default value. For example if it takes more than the retry time to get an answer from the function, the decorator will give up.
117
117
 
118
- ### `cache_raw` decorator helper
119
-
120
- No serializer or deserializer. This will only work if the cached function only returns `byte`, `str`, `int` or `float` types. Even `None` will fail.
121
-
122
- ### `cache_raw_wait` decorator helper
123
-
124
- Same as above but waits for the value if not in the cache.
125
-
126
- ### `cache_json` decorator helper
127
-
128
- Serialize the value with `json.dumps()` and deserialize the value with `json.loads()`.
129
-
130
- ### `cache_json_wait` decorator helper
131
-
132
- Same as above but waits for the value if not in the cache.
133
-
134
118
  ### `get_stats(delete=False)`
135
119
 
136
120
  This will get the stats stored when using the cache. The `delete` option is to reset the counters after read.
@@ -138,6 +122,7 @@ The output is a dictionary with the following keys and values:
138
122
 
139
123
  - **Refresh**: Number of times the cached function was actually called.
140
124
  - **Wait**: Number of times we waited for the result when executing the function.
125
+ - **Sleep**: Number of 1 seconds we waited for the results to be found in the cache.
141
126
  - **Failed**: Number of times the cached function raised an exception when called.
142
127
  - **Missed**: Number of times the functions result was not found in the cache.
143
128
  - **Success**: Number of times the function's result was found in the cache.
@@ -87,22 +87,6 @@ See `test_rediscache.py` for more examples.
87
87
 
88
88
  Note: when you choose to wait for the value, you do not have an absolute guarantee that you will not get the default value. For example if it takes more than the retry time to get an answer from the function, the decorator will give up.
89
89
 
90
- ### `cache_raw` decorator helper
91
-
92
- No serializer or deserializer. This will only work if the cached function only returns `byte`, `str`, `int` or `float` types. Even `None` will fail.
93
-
94
- ### `cache_raw_wait` decorator helper
95
-
96
- Same as above but waits for the value if not in the cache.
97
-
98
- ### `cache_json` decorator helper
99
-
100
- Serialize the value with `json.dumps()` and deserialize the value with `json.loads()`.
101
-
102
- ### `cache_json_wait` decorator helper
103
-
104
- Same as above but waits for the value if not in the cache.
105
-
106
90
  ### `get_stats(delete=False)`
107
91
 
108
92
  This will get the stats stored when using the cache. The `delete` option is to reset the counters after read.
@@ -110,6 +94,7 @@ The output is a dictionary with the following keys and values:
110
94
 
111
95
  - **Refresh**: Number of times the cached function was actually called.
112
96
  - **Wait**: Number of times we waited for the result when executing the function.
97
+ - **Sleep**: Number of 1 seconds we waited for the results to be found in the cache.
113
98
  - **Failed**: Number of times the cached function raised an exception when called.
114
99
  - **Missed**: Number of times the functions result was not found in the cache.
115
100
  - **Success**: Number of times the function's result was found in the cache.
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "rediscache"
3
- version = "0.3.3"
3
+ version = "1.0.0"
4
4
  description = "Redis caching of functions evolving over time"
5
5
  authors = ["Pierre Cart-Grandjean <pcart-grandjean@amadeus.com>"]
6
6
  license = "MIT"
@@ -46,3 +46,4 @@ strict = true
46
46
  [tool.pylint.format]
47
47
  # Maximum number of characters on a single line.
48
48
  max-line-length = 160
49
+ max-args = 10
@@ -40,10 +40,10 @@ import logging
40
40
  import os
41
41
  import threading
42
42
  from time import sleep
43
- from typing import Any, Callable, Dict, List, Optional, ParamSpec, TypeVar
43
+ from typing import Any, Callable, cast, Dict, List, Optional, ParamSpec, TypeVar
44
44
 
45
- import redis
46
45
  from executiontime import printexecutiontime, YELLOW, RED
46
+ import redis
47
47
 
48
48
  PREFIX = "."
49
49
  REFRESH = "Refresh" # Number of times the cached function was actually called.
@@ -56,7 +56,7 @@ DEFAULT = "Default" # Number of times the default value was used because nothin
56
56
  STATS = [REFRESH, WAIT, SLEEP, FAILED, MISSED, SUCCESS, DEFAULT]
57
57
 
58
58
  P = ParamSpec("P")
59
- T = TypeVar("T")
59
+ T = TypeVar("T", str, bytes)
60
60
 
61
61
 
62
62
  class RedisCache:
@@ -64,8 +64,6 @@ class RedisCache:
64
64
  Having the decorator provided by a class allows to have some context to improve performances.
65
65
  """
66
66
 
67
- # pylint: disable=too-many-arguments
68
-
69
67
  def __init__(
70
68
  self,
71
69
  host: Optional[str] = None,
@@ -120,25 +118,18 @@ class RedisCache:
120
118
 
121
119
  return f"{name}({','.join(values)})"
122
120
 
123
- # pylint: disable=line-too-long
124
121
  def cache(
125
122
  self,
126
123
  refresh: int,
127
124
  expire: int,
125
+ default: T,
128
126
  retry: Optional[int] = None,
129
- default: Any = "",
130
127
  wait: bool = False,
131
- serializer: Optional[Callable[..., Any]] = None,
132
- deserializer: Optional[Callable[..., Any]] = None,
133
128
  use_args: Optional[List[int]] = None,
134
129
  use_kwargs: Optional[List[str]] = None,
135
130
  ) -> Callable[[Callable[P, T]], Callable[P, T]]:
136
131
  """
137
- Full decorator will all possible parameters. Most of the time, you should use a specialized decorator below.
138
-
139
- Specific examples when to use this decorator:
140
- - Raw storage of byte string that you do not want to be decoded: use the decode=False.
141
- - JSON dumps data that doesn't need to be loaded before it is sent by a REST API: use serializer=dumps but no deserializer.
132
+ Full decorator will all possible parameters.
142
133
  """
143
134
 
144
135
  logger = logging.getLogger(__name__)
@@ -188,11 +179,8 @@ class RedisCache:
188
179
  self.server.incr(DEFAULT)
189
180
  new_value = default
190
181
 
191
- # Serialize the value if requested
192
- if serializer:
193
- new_value = serializer(new_value)
194
182
  # Store value in cache with expiration time
195
- self.server.set(key, new_value, ex=expire) # type: ignore
183
+ self.server.set(key, new_value, ex=expire)
196
184
  # Set refresh key with refresh time
197
185
  self.server.set(PREFIX + key, 1, ex=refresh)
198
186
  return new_value
@@ -206,13 +194,7 @@ class RedisCache:
206
194
 
207
195
  # If the cache is disabled, directly call the function
208
196
  if not self.enabled:
209
- direct_value = function(*args, **kwargs)
210
- # If we have decided to serialize, we always do it to be consistent
211
- if serializer:
212
- direct_value = serializer(direct_value)
213
- if deserializer:
214
- direct_value = deserializer(direct_value)
215
- return direct_value
197
+ return function(*args, **kwargs)
216
198
 
217
199
  # Lets create a key from the function's name and its parameters values
218
200
  key = self._create_key(name=function.__name__, args=args, use_args=use_args, kwargs=kwargs, use_kwargs=use_kwargs)
@@ -225,7 +207,7 @@ class RedisCache:
225
207
 
226
208
  # Get the value from the cache.
227
209
  # If it is not there we will get None.
228
- cached_value = self.server.get(key)
210
+ cached_value = cast(T, self.server.get(key))
229
211
 
230
212
  # Time to update stats counters
231
213
  if cached_value is None:
@@ -254,16 +236,16 @@ class RedisCache:
254
236
  # Let's count how many times we wait 1s
255
237
  self.server.incr(SLEEP)
256
238
  sleep(1)
257
- cached_value = self.server.get(key)
239
+ cached_value = cast(T, self.server.get(key))
258
240
 
259
241
  # If the cache was empty, we have None in the cached_value.
260
242
  if cached_value is None:
261
243
  # We are going to return the default value
262
244
  self.server.incr(DEFAULT)
263
- cached_value = serializer(default) if serializer else default
245
+ cached_value = default
264
246
 
265
247
  # Return whatever value we have at this point.
266
- return deserializer(cached_value) if deserializer else cached_value # type: ignore
248
+ return cached_value
267
249
 
268
250
  # If we want to bypass the cache at runtime, we need a reference to the decorated function
269
251
  wrapper.function = function # type: ignore
@@ -272,45 +254,6 @@ class RedisCache:
272
254
 
273
255
  return decorator
274
256
 
275
- def cache_raw(self, refresh: int, expire: int, retry: Optional[int] = None, default: Any = "") -> Callable[[Callable[P, T]], Callable[P, T]]:
276
- """
277
- Normal caching of values directly storable in redis: byte string, string, int, float.
278
- """
279
- return self.cache(refresh=refresh, expire=expire, retry=retry, default=default)
280
-
281
- def cache_raw_wait(self, refresh: int, expire: int, retry: Optional[int] = None, default: Any = "") -> Callable[[Callable[P, T]], Callable[P, T]]:
282
- """
283
- Same as cache_raw() but will wait for the completion of the cached function if no value is found in redis.
284
- """
285
- return self.cache(refresh=refresh, expire=expire, retry=retry, default=default, wait=True)
286
-
287
- def cache_json(self, refresh: int, expire: int, retry: Optional[int] = None, default: Any = "") -> Callable[[Callable[P, T]], Callable[P, T]]:
288
- """
289
- JSON dumps the values to be stored in redis and loads them again when returning them to the caller.
290
- """
291
- return self.cache(
292
- refresh=refresh,
293
- expire=expire,
294
- retry=retry,
295
- default=default,
296
- serializer=dumps,
297
- deserializer=loads,
298
- )
299
-
300
- def cache_json_wait(self, refresh: int, expire: int, retry: Optional[int] = None, default: Any = "") -> Callable[[Callable[P, T]], Callable[P, T]]:
301
- """
302
- Same as cache_json() but will wait for the completion of the cached function if no value is found in redis.
303
- """
304
- return self.cache(
305
- refresh=refresh,
306
- expire=expire,
307
- retry=retry,
308
- default=default,
309
- wait=True,
310
- serializer=dumps,
311
- deserializer=loads,
312
- )
313
-
314
257
  def get_stats(self, delete: bool = False) -> Dict[str, Any]:
315
258
  """
316
259
  Get the stats stored by RedisCache. See the list and definition at the top of this file.
File without changes