Skip to content

Commit 03b7438

Browse files
author
Fabien Coelho
committed
9.3
1 parent 9ffff45 commit 03b7438

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

CacheToolsUtils.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,10 @@ def cached(cache, *args, **kwargs):
380380

381381
def decorate(fun: Callable):
382382

383-
# use cachetools
383+
# use cachetools "cached" decorator
384384
fun = cachetools.cached(cache, *args, **kwargs)(fun)
385385

386-
# extend
386+
# extend it with two functions
387387
def cache_in(*args, **kwargs) -> bool:
388388
"""Tell whether key is already in cache."""
389389
key = fun.cache_key(*args, **kwargs) # type: ignore
@@ -409,12 +409,19 @@ def cache_del(*args, **kwargs):
409409
MapGen = Callable[[MutableMapping, str], MutableMapping]
410410

411411

412-
def cacheMethods(cache: MutableMapping, obj: Any, gen: MapGen = PrefixedCache, **funs):
412+
def cacheMethods(
413+
cache: MutableMapping,
414+
obj: Any,
415+
gen: MapGen = PrefixedCache,
416+
opts: dict[str, Any] = {},
417+
**funs
418+
):
413419
"""Cache some object methods.
414420
415421
:param cache: cache to use.
416422
:param obj: object instance to be cached.
417423
:param gen: generator of PrefixedCache.
424+
:param opts: additional parameters when calling `cached`.
418425
:param funs: name of methods and corresponding prefix
419426
420427
.. code-block:: python
@@ -426,17 +433,22 @@ def cacheMethods(cache: MutableMapping, obj: Any, gen: MapGen = PrefixedCache, *
426433
f = getattr(obj, fun)
427434
while hasattr(f, "__wrapped__"):
428435
f = f.__wrapped__
429-
setattr(obj, fun, cached(cache=gen(cache, prefix))(f))
436+
setattr(obj, fun, cached(cache=gen(cache, prefix), **opts)(f))
430437

431438

432439
def cacheFunctions(
433-
cache: MutableMapping, globs: MutableMapping[str, Any], gen: MapGen = PrefixedCache, **funs
440+
cache: MutableMapping,
441+
globs: MutableMapping[str, Any],
442+
gen: MapGen = PrefixedCache,
443+
opts: dict[str, Any] = {},
444+
**funs
434445
):
435446
"""Cache some global functions, with a prefix.
436447
437448
:param cache: cache to use.
438449
:param globs: global object dictionary.
439450
:param gen: generator of PrefixedCache.
451+
:param opts: additional parameters when calling `cached`.
440452
:param funs: name of functions and corresponding prefix
441453
442454
.. code-block:: python
@@ -448,7 +460,7 @@ def cacheFunctions(
448460
f = globs[fun]
449461
while hasattr(f, "__wrapped__"):
450462
f = f.__wrapped__
451-
globs[fun] = cached(cache=gen(cache, prefix))(f)
463+
globs[fun] = cached(cache=gen(cache, prefix), **opts)(f)
452464

453465

454466
# JSON-based key function

docs/REFERENCE.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ pcache = ctu.PrefixedRedisCache(rd_base, "pac.", ttl=3600)
137137
This utility function create a prefixed cache around methods of an object
138138
or functions in the global scope.
139139
First parameter is the actual cache, second parameter is the object or scope,
140+
`opts` named-parameter allows additional options to `cached`,
140141
and finally a keyword mapping from function names to prefixes.
141142

142143
```python
143144
# add cache to obj.get_data and obj.get_some
144145
ctu.cacheMethods(cache, obj, get_data="1.", get_some="2.")
145146

146147
# add cache to some_func
147-
ctu.cacheFunctions(cache, globals(), some_func="f.")
148+
ctu.cacheFunctions(cache, globals(), opts={"key": ctu.json_key}, some_func="f.")
148149
```
149150

150151
## Decorator cached

docs/VERSIONS.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Install [package](https://pypi.org/project/CacheToolsUtils/) from
1919
Maybe the existing client can do that with appropriate options?
2020
- `cached`: add `contains` and `delete` parameters to change names?
2121

22+
## 9.3 on 2024-12-06
23+
24+
Add `opts` to provide additional parameters to cached when wrapping methods
25+
and functions.
26+
2227
## 9.2 on 2024-11-29
2328

2429
Simpler and more compact json cache key function.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "CacheToolsUtils"
7-
version = "9.2"
7+
version = "9.3"
88
authors = [ { name = "Fabien Coelho", email = "[email protected]" } ]
99
description = "Cachetools Utilities"
1010
readme = "README.md"

test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def test_methods():
392392
s = Stuff("test_methods")
393393
c = ct.TTLCache(1024, ttl=60)
394394
cs = ctu.StatsCache(c)
395-
ctu.cacheMethods(cs, s, sum_n1="1.", sum_n2="2.")
395+
ctu.cacheMethods(cs, s, opts={"key": ctu.json_key}, sum_n1="1.", sum_n2="2.")
396396
n2 = s.sum_n2(128)
397397
n1 = s.sum_n1(128)
398398
for i in range(1, 128):

0 commit comments

Comments
 (0)