@@ -215,8 +215,10 @@ def __delitem__(self, key):
215
215
class PrefixedCache (_KeyMutMapMix , _StatsMix , MutableMapping ):
216
216
"""Cache class to add a key prefix.
217
217
218
- :param cache: actual cache.
219
- :param prefix: prefix to prepend to keys.
218
+ Parameters:
219
+
220
+ - cache: actual cache.
221
+ - prefix: prefix to prepend to keys.
220
222
"""
221
223
222
224
def __init__ (self , cache : MutableMapping , prefix : str | bytes = "" ):
@@ -236,9 +238,11 @@ def _key(self, key: Any) -> Any:
236
238
class AutoPrefixedCache (PrefixedCache ):
237
239
"""Cache class with an automatic counter-based string prefix.
238
240
239
- :param cache: actual cache.
240
- :param sep: prefix separator, default is ".".
241
- :param method: encoding method in "b64", "b64u", "b32", "b32x", "b16",
241
+ Parameters:
242
+
243
+ - cache: actual cache.
244
+ - sep: prefix separator, default is ".".
245
+ - method: encoding method in "b64", "b64u", "b32", "b32x", "b16",
242
246
"a85" and "b85". Default is "b64".
243
247
"""
244
248
@@ -269,16 +273,17 @@ def __init__(self, cache: MutableMapping, sep: str = ".", method: str = "b64"):
269
273
class StatsCache (_MutMapMix , MutableMapping ):
270
274
"""Cache class to keep stats.
271
275
272
- :param cache: actual cache.
276
+ Parameter:
273
277
274
- .. code-block: python
278
+ - cache: actual cache.
275
279
276
- import cachetools as ct
277
- import CacheToolsUtils as ctu
278
- cache = ctu.StatsCache(ct.LRUCache())
279
- ...
280
+ ```python
281
+ import cachetools as ct
282
+ import CacheToolsUtils as ctu
283
+ cache = ctu.StatsCache(ct.LRUCache())
284
+ ```
280
285
281
- Note that CacheTools `` cached`` decorator with `` info=True` ` provides
286
+ Note that CacheTools `cached` decorator with `info=True` provides
282
287
hits, misses, maxsize and currsize information.
283
288
However, this only works for its own classes.
284
289
"""
@@ -333,9 +338,11 @@ def clear(self):
333
338
class TwoLevelCache (_MutMapMix , MutableMapping ):
334
339
"""Two-Level Cache class for CacheTools.
335
340
336
- :param cache: first (smaller, shorter TTL) cache
337
- :param cache2: second (larger, longer TTL) cache
338
- :param resilient: whether to ignore cache2 failures
341
+ Parameters:
342
+
343
+ - cache: first (smaller, shorter TTL) cache
344
+ - cache2: second (larger, longer TTL) cache
345
+ - resilient: whether to ignore cache2 failures
339
346
"""
340
347
341
348
def __init__ (self , cache : MutableMapping , cache2 : MutableMapping , resilient = False ):
@@ -461,10 +468,12 @@ def new(self, derived: bytes):
461
468
class EncryptedCache (_KeyMutMapMix , _StatsMix , MutableMapping ):
462
469
"""Encrypted Bytes Key-Value Cache.
463
470
464
- :param secret: bytes of secret, at least 16 bytes.
465
- :param hsize: size of hashed key, default is *16*.
466
- :param csize: value checksum size, default is *0*.
467
- :param cipher: chose cipher from "Salsa20", "AES-128-CBC" or "ChaCha20".
471
+ Parameters:
472
+
473
+ - secret: bytes of secret, at least 16 bytes.
474
+ - hsize: size of hashed key, default is *16*.
475
+ - csize: value checksum size, default is *0*.
476
+ - cipher: chose cipher from "Salsa20", "AES-128-CBC" or "ChaCha20".
468
477
469
478
The key is *not* encrypted but simply hashed, thus they are
470
479
fixed size with a very low collision probability.
@@ -557,14 +566,16 @@ def __getitem__(self, key):
557
566
def cached (cache , * args , ** kwargs ):
558
567
"""Extended decorator with delete and exists.
559
568
560
- All parameters are forwarded to ``cachetools.cached``.
569
+ All parameters are forwarded to `cachetools.cached`.
570
+
571
+ Parameter:
561
572
562
- :param cache: actual cache.
573
+ - cache: actual cache.
563
574
564
- If *f(\\ *args, \\ *\\ *kwargs)* is the ``cached`` function, then:
575
+ If *f(\\ *args, \\ *\\ *kwargs)* is the _cached_ function, then:
565
576
566
- - `` f.cache_in(*args, **kwargs)` ` tells whether the result is cached.
567
- - `` f.cache_del(*args, **kwargs)` ` deletes (invalidates) the cached result.
577
+ - `f.cache_in(*args, **kwargs)` tells whether the result is cached.
578
+ - `f.cache_del(*args, **kwargs)` deletes (invalidates) the cached result.
568
579
"""
569
580
570
581
def decorate (fun : Callable ):
@@ -607,15 +618,17 @@ def cacheMethods(
607
618
):
608
619
"""Cache some object methods.
609
620
610
- :param cache: cache to use.
611
- :param obj: object instance to be cached.
612
- :param gen: generator of PrefixedCache.
613
- :param opts: additional parameters when calling `cached`.
614
- :param funs: name of methods and corresponding prefix
621
+ Parameters:
615
622
616
- .. code-block:: python
623
+ - cache: cache to use.
624
+ - obj: object instance to be cached.
625
+ - gen: generator of PrefixedCache.
626
+ - opts: additional parameters when calling `cached`.
627
+ - funs: name of methods and corresponding prefix
617
628
618
- cacheMethods(cache, item, PrefixedCache, compute1="c1.", compute2="c2.")
629
+ ```python
630
+ cacheMethods(cache, item, PrefixedCache, compute1="c1.", compute2="c2.")
631
+ ```
619
632
"""
620
633
for fun , prefix in funs .items ():
621
634
assert hasattr (obj , fun ), f"cannot cache missing method { fun } on { obj } "
@@ -634,15 +647,17 @@ def cacheFunctions(
634
647
):
635
648
"""Cache some global functions, with a prefix.
636
649
637
- :param cache: cache to use.
638
- :param globs: global object dictionary.
639
- :param gen: generator of PrefixedCache.
640
- :param opts: additional parameters when calling `cached`.
641
- :param funs: name of functions and corresponding prefix
650
+ Parameters:
642
651
643
- .. code-block:: python
652
+ - cache: cache to use.
653
+ - globs: global object dictionary.
654
+ - gen: generator of PrefixedCache.
655
+ - opts: additional parameters when calling `cached`.
656
+ - funs: name of functions and corresponding prefix
644
657
645
- cacheFunctions(cache, globals(), PrefixedCache, fun1="f1.", fun2="f2.")
658
+ ```python
659
+ cacheFunctions(cache, globals(), PrefixedCache, fun1="f1.", fun2="f2.")
660
+ ```
646
661
"""
647
662
for fun , prefix in funs .items ():
648
663
assert fun in globs , "caching functions: {fun} not found"
@@ -701,13 +716,13 @@ def full_hash_key(*args, **kwargs) -> str:
701
716
# MEMCACHED
702
717
#
703
718
class JsonSerde :
704
- """JSON serialize/deserialize class for MemCached (``pymemcache``).
705
-
706
- .. code-block:: python
719
+ """JSON serialize/deserialize class for MemCached (`pymemcache`).
707
720
708
- import pymemcache as pmc
709
- import CacheToolsUtils as ctu
710
- pmc_cache = pmc.Client(server="localhost", serde=ctu.JsonSerde())
721
+ ```python
722
+ import pymemcache as pmc
723
+ import CacheToolsUtils as ctu
724
+ pmc_cache = pmc.Client(server="localhost", serde=ctu.JsonSerde())
725
+ ```
711
726
"""
712
727
713
728
# keep strings, else json
@@ -734,16 +749,19 @@ def deserialize(self, key, value, flag):
734
749
class MemCached (_KeyMutMapMix , MutableMapping ):
735
750
"""MemCached-compatible wrapper class for cachetools with key encoding.
736
751
737
- :param cache: actual memcached cache.
752
+ Parameter:
738
753
739
- .. code-block:: python
754
+ - cache: actual memcached cache.
740
755
741
- import pymemcache as pmc
742
- import CacheToolsUtils as ctu
743
- cache = ctu.MemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()))
756
+ ```python
757
+ import pymemcache as pmc
758
+ import CacheToolsUtils as ctu
759
+ cache = ctu.MemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()))
744
760
745
- @ctu.cached(cache=cache)
746
- def whatever(...):
761
+ @ctu.cached(cache=cache)
762
+ def whatever(...):
763
+ ...
764
+ ```
747
765
"""
748
766
749
767
def __init__ (self , cache ):
@@ -777,15 +795,17 @@ def hits(self) -> float:
777
795
class PrefixedMemCached (MemCached ):
778
796
"""MemCached-compatible wrapper class for cachetools with a key prefix.
779
797
780
- :param cache: actual memcached cache.
781
- :param prefix: post key-encoding prepended prefix.
798
+ Parameters:
782
799
783
- .. code-block:: python
800
+ - cache: actual memcached cache.
801
+ - prefix: post key-encoding prepended prefix.
784
802
785
- import pymemcache as pmc
786
- import CacheToolsUtils as ctu
787
- # add a "app." prefix to keys, after serialization
788
- cache = ctu.PrefixedMemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()), "app.")
803
+ ```python
804
+ import pymemcache as pmc
805
+ import CacheToolsUtils as ctu
806
+ # add a "app." prefix to keys, after serialization
807
+ cache = ctu.PrefixedMemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()), "app.")
808
+ ```
789
809
"""
790
810
791
811
def __init__ (self , cache , prefix : str = "" ):
@@ -800,20 +820,22 @@ def _key(self, key):
800
820
# REDIS
801
821
#
802
822
class RedisCache (MutableMapping ):
803
- """Redis TTL-ed wrapper for cachetools (`` redis` `).
823
+ """Redis TTL-ed wrapper for cachetools (`redis`).
804
824
805
- :param cache: actual redis cache.
806
- :param ttl: time-to-live in seconds, used as default expiration (``ex``), default is 600.
807
- :param raw: whether to serialize keys and values, default is *False*.
825
+ Parameters:
808
826
809
- Keys and values are serialized in *JSON*.
827
+ - cache: actual redis cache.
828
+ - ttl: time-to-live in seconds, used as default expiration (`ex`), default is _600_.
829
+ - raw: whether to serialize keys and values, default is *False*.
810
830
811
- .. code-block:: python
831
+ Keys and values are serialized in *JSON*.
812
832
813
- import redis
814
- import CacheToolsUtils as ctu
815
- # redis with 1 hour expiration
816
- cache = ctu.RedisCache(redis.Redis(host="localhost"), 3600)
833
+ ```python
834
+ import redis
835
+ import CacheToolsUtils as ctu
836
+ # redis with 1 hour expiration
837
+ cache = ctu.RedisCache(redis.Redis(host="localhost"), 3600)
838
+ ```
817
839
"""
818
840
819
841
def __init__ (self , cache , ttl = 600 , raw = False ):
@@ -893,16 +915,18 @@ def hits(self) -> float:
893
915
class PrefixedRedisCache (RedisCache ):
894
916
"""Prefixed Redis wrapper class for cachetools.
895
917
896
- :param cache: actual redis cache.
897
- :param prefix: post key encoding prefix, default is empty.
898
- :param ttl: time-to-live in seconds, used as default expiration (``ex``), default is 600.
918
+ Parameters:
899
919
900
- .. code-block:: python
920
+ - cache: actual redis cache.
921
+ - prefix: post key encoding prefix, default is empty.
922
+ - ttl: time-to-live in seconds, used as default expiration (`ex`), default is _600_.
901
923
902
- import redis
903
- import CacheToolsUtils as ctu
904
- # redis with "app." prefix and 1 hour expiration
905
- cache = ctu.PrefixedRedisCache(redis.Redis(host="localhost"), "app.", 3600)
924
+ ```python
925
+ import redis
926
+ import CacheToolsUtils as ctu
927
+ # redis with "app." prefix and 1 hour expiration
928
+ cache = ctu.PrefixedRedisCache(redis.Redis(host="localhost"), "app.", 3600)
929
+ ```
906
930
"""
907
931
908
932
def __init__ (self , cache , prefix : str = "" , ttl = 600 ):
0 commit comments