@@ -380,10 +380,10 @@ def cached(cache, *args, **kwargs):
380
380
381
381
def decorate (fun : Callable ):
382
382
383
- # use cachetools
383
+ # use cachetools "cached" decorator
384
384
fun = cachetools .cached (cache , * args , ** kwargs )(fun )
385
385
386
- # extend
386
+ # extend it with two functions
387
387
def cache_in (* args , ** kwargs ) -> bool :
388
388
"""Tell whether key is already in cache."""
389
389
key = fun .cache_key (* args , ** kwargs ) # type: ignore
@@ -409,12 +409,19 @@ def cache_del(*args, **kwargs):
409
409
MapGen = Callable [[MutableMapping , str ], MutableMapping ]
410
410
411
411
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
+ ):
413
419
"""Cache some object methods.
414
420
415
421
:param cache: cache to use.
416
422
:param obj: object instance to be cached.
417
423
:param gen: generator of PrefixedCache.
424
+ :param opts: additional parameters when calling `cached`.
418
425
:param funs: name of methods and corresponding prefix
419
426
420
427
.. code-block:: python
@@ -426,17 +433,22 @@ def cacheMethods(cache: MutableMapping, obj: Any, gen: MapGen = PrefixedCache, *
426
433
f = getattr (obj , fun )
427
434
while hasattr (f , "__wrapped__" ):
428
435
f = f .__wrapped__
429
- setattr (obj , fun , cached (cache = gen (cache , prefix ))(f ))
436
+ setattr (obj , fun , cached (cache = gen (cache , prefix ), ** opts )(f ))
430
437
431
438
432
439
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
434
445
):
435
446
"""Cache some global functions, with a prefix.
436
447
437
448
:param cache: cache to use.
438
449
:param globs: global object dictionary.
439
450
:param gen: generator of PrefixedCache.
451
+ :param opts: additional parameters when calling `cached`.
440
452
:param funs: name of functions and corresponding prefix
441
453
442
454
.. code-block:: python
@@ -448,7 +460,7 @@ def cacheFunctions(
448
460
f = globs [fun ]
449
461
while hasattr (f , "__wrapped__" ):
450
462
f = f .__wrapped__
451
- globs [fun ] = cached (cache = gen (cache , prefix ))(f )
463
+ globs [fun ] = cached (cache = gen (cache , prefix ), ** opts )(f )
452
464
453
465
454
466
# JSON-based key function
0 commit comments