rediscache 1.0.3__tar.gz → 1.0.5__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.
- {rediscache-1.0.3 → rediscache-1.0.5}/PKG-INFO +2 -4
- {rediscache-1.0.3 → rediscache-1.0.5}/README.md +1 -3
- {rediscache-1.0.3 → rediscache-1.0.5}/pyproject.toml +1 -1
- {rediscache-1.0.3 → rediscache-1.0.5}/rediscache/__init__.py +32 -10
- rediscache-1.0.5/rediscache/py.typed +0 -0
- {rediscache-1.0.3 → rediscache-1.0.5}/rediscache/tools.py +4 -0
- {rediscache-1.0.3 → rediscache-1.0.5}/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: rediscache
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.5
|
|
4
4
|
Summary: Redis caching of functions evolving over time
|
|
5
5
|
License: MIT
|
|
6
6
|
Keywords: redis,performance,cache
|
|
@@ -92,11 +92,9 @@ This is the main decorator. All the parameters are available. The mandatory ones
|
|
|
92
92
|
|
|
93
93
|
- refresh: The amount of seconds before it would be a good idea to refresh the cached value.
|
|
94
94
|
- expire: How many seconds that the value in the cache is still considered good enough to be sent back to the caller.
|
|
95
|
-
- retry: While a value is being refreshed, we want to avoid to refresh it in parallel. But if it is taking too long, after the number of seconds provided here, we may want to try our luck again. If not specified, we will take the `refresh` value.
|
|
96
95
|
- default: If we do not have the value in the cache and we do not want to wait, what shall we send back to the caller? It has to be serializable because it will also be stored in the cache. [`''`]
|
|
96
|
+
- retry: While a value is being refreshed, we want to avoid to refresh it in parallel. But if it is taking too long, after the number of seconds provided here, we may want to try our luck again. If not specified, we will take the `refresh` value.
|
|
97
97
|
- wait: If the value is not in the cache, do we wait for the return of the function? [`False`]
|
|
98
|
-
- serializer: The only type of data that can be stored directly in the Redis database are `byte`, `str`, `int` and `float`. Any other will have to be serialized with the function provided here. [`None`]
|
|
99
|
-
- deserializer: If the value was serialized to be stored in the cache, it needs to deserialized when it is retrieved. [`None`]
|
|
100
98
|
- use_args: This is the list of positional parameters (a list of integers) to be taken into account to generate the key that will be used in Redis. If `None`, they will all be used. [`None`]
|
|
101
99
|
- use_kwargs: This is the list of named parameters (a list of names) to be taken into account to generate the key that will be used in Redis. If `None`, they will all be used. [`None`]
|
|
102
100
|
|
|
@@ -64,11 +64,9 @@ This is the main decorator. All the parameters are available. The mandatory ones
|
|
|
64
64
|
|
|
65
65
|
- refresh: The amount of seconds before it would be a good idea to refresh the cached value.
|
|
66
66
|
- expire: How many seconds that the value in the cache is still considered good enough to be sent back to the caller.
|
|
67
|
-
- retry: While a value is being refreshed, we want to avoid to refresh it in parallel. But if it is taking too long, after the number of seconds provided here, we may want to try our luck again. If not specified, we will take the `refresh` value.
|
|
68
67
|
- default: If we do not have the value in the cache and we do not want to wait, what shall we send back to the caller? It has to be serializable because it will also be stored in the cache. [`''`]
|
|
68
|
+
- retry: While a value is being refreshed, we want to avoid to refresh it in parallel. But if it is taking too long, after the number of seconds provided here, we may want to try our luck again. If not specified, we will take the `refresh` value.
|
|
69
69
|
- wait: If the value is not in the cache, do we wait for the return of the function? [`False`]
|
|
70
|
-
- serializer: The only type of data that can be stored directly in the Redis database are `byte`, `str`, `int` and `float`. Any other will have to be serialized with the function provided here. [`None`]
|
|
71
|
-
- deserializer: If the value was serialized to be stored in the cache, it needs to deserialized when it is retrieved. [`None`]
|
|
72
70
|
- use_args: This is the list of positional parameters (a list of integers) to be taken into account to generate the key that will be used in Redis. If `None`, they will all be used. [`None`]
|
|
73
71
|
- use_kwargs: This is the list of named parameters (a list of names) to be taken into account to generate the key that will be used in Redis. If `None`, they will all be used. [`None`]
|
|
74
72
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# For more details on this file see: https://python-poetry.org/docs/pyproject/
|
|
2
2
|
[tool.poetry]
|
|
3
3
|
name = "rediscache"
|
|
4
|
-
version = "1.0.
|
|
4
|
+
version = "1.0.5"
|
|
5
5
|
description = "Redis caching of functions evolving over time"
|
|
6
6
|
authors = ["Pierre Cart-Grandjean <pcart-grandjean@amadeus.com>"]
|
|
7
7
|
license = "MIT"
|
|
@@ -63,7 +63,7 @@ class RedisCache:
|
|
|
63
63
|
Having the decorator provided by a class allows to have some context to improve performances.
|
|
64
64
|
"""
|
|
65
65
|
|
|
66
|
-
def __init__(
|
|
66
|
+
def __init__( # pylint: disable=too-many-positional-arguments
|
|
67
67
|
self,
|
|
68
68
|
host: Optional[str] = None,
|
|
69
69
|
port: Optional[int] = None,
|
|
@@ -72,6 +72,16 @@ class RedisCache:
|
|
|
72
72
|
decode: bool = True,
|
|
73
73
|
enabled: bool = True,
|
|
74
74
|
):
|
|
75
|
+
"""
|
|
76
|
+
Provide configuration parameter to a RedisCache instance.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
host: The host of the Redis server instance to be used.
|
|
80
|
+
port: The port the Redis server listens to.
|
|
81
|
+
db: The name of the database to be used if not default.
|
|
82
|
+
decode: If true, decode the data stored in the cache as byte string.
|
|
83
|
+
enabled: When False it allows to programmatically disable the cache.
|
|
84
|
+
"""
|
|
75
85
|
self.enabled = enabled
|
|
76
86
|
if self.enabled:
|
|
77
87
|
# If environment variables are set for redis server, they supersede the default values.
|
|
@@ -87,7 +97,7 @@ class RedisCache:
|
|
|
87
97
|
password = os.environ.get("REDIS_SERVICE_PASSWORD")
|
|
88
98
|
self.server = redis.StrictRedis(host=host, port=port, db=db, password=password, decode_responses=decode)
|
|
89
99
|
|
|
90
|
-
def _create_key(
|
|
100
|
+
def _create_key( # pylint: disable=too-many-positional-arguments
|
|
91
101
|
self,
|
|
92
102
|
name: str,
|
|
93
103
|
args: Optional[tuple[Any, ...]] = None,
|
|
@@ -117,7 +127,7 @@ class RedisCache:
|
|
|
117
127
|
|
|
118
128
|
return f"{name}({','.join(values)})"
|
|
119
129
|
|
|
120
|
-
def cache(
|
|
130
|
+
def cache( # pylint: disable=too-many-positional-arguments
|
|
121
131
|
self,
|
|
122
132
|
refresh: int,
|
|
123
133
|
expire: int,
|
|
@@ -128,7 +138,19 @@ class RedisCache:
|
|
|
128
138
|
use_kwargs: Optional[List[str]] = None,
|
|
129
139
|
) -> Callable[[Callable[P, T]], Callable[P, T]]:
|
|
130
140
|
"""
|
|
131
|
-
Full decorator
|
|
141
|
+
Full decorator with all possible parameters.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
refresh: The amount of seconds before it would be a good idea to refresh the cached value.
|
|
145
|
+
expire: How many seconds that the value in the cache is still considered good enough to be sent back to the caller.
|
|
146
|
+
default: If we do not have the value in the cache and we do not want to wait, what shall we send back to the caller?
|
|
147
|
+
It has to be serializable because it will also be stored in the cache.
|
|
148
|
+
retry: While a value is being refreshed, we want to avoid to refresh it in parallel.
|
|
149
|
+
But if it is taking too long, after the number of seconds provided here, we may want to try our luck again.
|
|
150
|
+
If not specified, we will take the `refresh` value.
|
|
151
|
+
wait: If the value is not in the cache, do we wait for the return of the function?
|
|
152
|
+
use_args: This is the list of positional parameters (a list of integers) to be taken into account to generate the key that will be used in Redis.
|
|
153
|
+
use_kwargs: This is the list of named parameters (a list of names) to be taken into account to generate the key that will be used in Redis.
|
|
132
154
|
"""
|
|
133
155
|
|
|
134
156
|
logger = logging.getLogger(__name__)
|
|
@@ -197,12 +219,6 @@ class RedisCache:
|
|
|
197
219
|
|
|
198
220
|
# Lets create a key from the function's name and its parameters values
|
|
199
221
|
key = self._create_key(name=function.__name__, args=args, use_args=use_args, kwargs=kwargs, use_kwargs=use_kwargs)
|
|
200
|
-
values = ",".join([str(value) for value in args])
|
|
201
|
-
dict_values = ",".join([str(key) + "='" + str(value) + "'" for key, value in kwargs.items()])
|
|
202
|
-
all_args = values
|
|
203
|
-
if values and dict_values:
|
|
204
|
-
all_args += ","
|
|
205
|
-
all_args += dict_values
|
|
206
222
|
|
|
207
223
|
# Get the value from the cache.
|
|
208
224
|
# If it is not there we will get None.
|
|
@@ -259,6 +275,12 @@ class RedisCache:
|
|
|
259
275
|
If delete is set to True we delete the stats from Redis after read.
|
|
260
276
|
From Redis 6.2, it is possible to GETDEL, making sure that we do not lose some data between
|
|
261
277
|
the 'get' and the 'delete'. But it is not available in the Redis (v3.5.3) python interface yet.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
delete: Reset the counters after read.
|
|
281
|
+
|
|
282
|
+
Returns:
|
|
283
|
+
dict: Dictionary of all the counters and their value.
|
|
262
284
|
"""
|
|
263
285
|
stats = {stat: self.server.get(stat) for stat in STATS}
|
|
264
286
|
if delete:
|
|
File without changes
|
|
@@ -17,6 +17,10 @@ def decorate(transform: Callable[[InT], OutT]) -> Callable[[Callable[P, InT]], C
|
|
|
17
17
|
It is especially meant to be used to serialize the output of a function to be cached.
|
|
18
18
|
It can also be used to deserialize the cached value, but this should be used with great caution
|
|
19
19
|
since it could be worse than not caching the function at all.
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
transform: the function that will take the output of the decorated function and transform it,
|
|
23
|
+
usually to a new type.
|
|
20
24
|
"""
|
|
21
25
|
|
|
22
26
|
def decorator(function: Callable[P, InT]) -> Callable[P, OutT]:
|
|
File without changes
|