Skip to content

Commit 6b3aaac

Browse files
author
Fabien Coelho
committed
rst to md
1 parent e4aa09e commit 6b3aaac

File tree

1 file changed

+100
-76
lines changed

1 file changed

+100
-76
lines changed

CacheToolsUtils.py

+100-76
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ def __delitem__(self, key):
215215
class PrefixedCache(_KeyMutMapMix, _StatsMix, MutableMapping):
216216
"""Cache class to add a key prefix.
217217
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.
220222
"""
221223

222224
def __init__(self, cache: MutableMapping, prefix: str|bytes = ""):
@@ -236,9 +238,11 @@ def _key(self, key: Any) -> Any:
236238
class AutoPrefixedCache(PrefixedCache):
237239
"""Cache class with an automatic counter-based string prefix.
238240
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",
242246
"a85" and "b85". Default is "b64".
243247
"""
244248

@@ -269,16 +273,17 @@ def __init__(self, cache: MutableMapping, sep: str = ".", method: str = "b64"):
269273
class StatsCache(_MutMapMix, MutableMapping):
270274
"""Cache class to keep stats.
271275
272-
:param cache: actual cache.
276+
Parameter:
273277
274-
.. code-block: python
278+
- cache: actual cache.
275279
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+
```
280285
281-
Note that CacheTools ``cached`` decorator with ``info=True`` provides
286+
Note that CacheTools `cached` decorator with `info=True` provides
282287
hits, misses, maxsize and currsize information.
283288
However, this only works for its own classes.
284289
"""
@@ -333,9 +338,11 @@ def clear(self):
333338
class TwoLevelCache(_MutMapMix, MutableMapping):
334339
"""Two-Level Cache class for CacheTools.
335340
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
339346
"""
340347

341348
def __init__(self, cache: MutableMapping, cache2: MutableMapping, resilient=False):
@@ -461,10 +468,12 @@ def new(self, derived: bytes):
461468
class EncryptedCache(_KeyMutMapMix, _StatsMix, MutableMapping):
462469
"""Encrypted Bytes Key-Value Cache.
463470
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".
468477
469478
The key is *not* encrypted but simply hashed, thus they are
470479
fixed size with a very low collision probability.
@@ -557,14 +566,16 @@ def __getitem__(self, key):
557566
def cached(cache, *args, **kwargs):
558567
"""Extended decorator with delete and exists.
559568
560-
All parameters are forwarded to ``cachetools.cached``.
569+
All parameters are forwarded to `cachetools.cached`.
570+
571+
Parameter:
561572
562-
:param cache: actual cache.
573+
- cache: actual cache.
563574
564-
If *f(\\*args, \\*\\*kwargs)* is the ``cached`` function, then:
575+
If *f(\\*args, \\*\\*kwargs)* is the _cached_ function, then:
565576
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.
568579
"""
569580

570581
def decorate(fun: Callable):
@@ -607,15 +618,17 @@ def cacheMethods(
607618
):
608619
"""Cache some object methods.
609620
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:
615622
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
617628
618-
cacheMethods(cache, item, PrefixedCache, compute1="c1.", compute2="c2.")
629+
```python
630+
cacheMethods(cache, item, PrefixedCache, compute1="c1.", compute2="c2.")
631+
```
619632
"""
620633
for fun, prefix in funs.items():
621634
assert hasattr(obj, fun), f"cannot cache missing method {fun} on {obj}"
@@ -634,15 +647,17 @@ def cacheFunctions(
634647
):
635648
"""Cache some global functions, with a prefix.
636649
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:
642651
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
644657
645-
cacheFunctions(cache, globals(), PrefixedCache, fun1="f1.", fun2="f2.")
658+
```python
659+
cacheFunctions(cache, globals(), PrefixedCache, fun1="f1.", fun2="f2.")
660+
```
646661
"""
647662
for fun, prefix in funs.items():
648663
assert fun in globs, "caching functions: {fun} not found"
@@ -701,13 +716,13 @@ def full_hash_key(*args, **kwargs) -> str:
701716
# MEMCACHED
702717
#
703718
class JsonSerde:
704-
"""JSON serialize/deserialize class for MemCached (``pymemcache``).
705-
706-
.. code-block:: python
719+
"""JSON serialize/deserialize class for MemCached (`pymemcache`).
707720
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+
```
711726
"""
712727

713728
# keep strings, else json
@@ -734,16 +749,19 @@ def deserialize(self, key, value, flag):
734749
class MemCached(_KeyMutMapMix, MutableMapping):
735750
"""MemCached-compatible wrapper class for cachetools with key encoding.
736751
737-
:param cache: actual memcached cache.
752+
Parameter:
738753
739-
.. code-block:: python
754+
- cache: actual memcached cache.
740755
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()))
744760
745-
@ctu.cached(cache=cache)
746-
def whatever(...):
761+
@ctu.cached(cache=cache)
762+
def whatever(...):
763+
...
764+
```
747765
"""
748766

749767
def __init__(self, cache):
@@ -777,15 +795,17 @@ def hits(self) -> float:
777795
class PrefixedMemCached(MemCached):
778796
"""MemCached-compatible wrapper class for cachetools with a key prefix.
779797
780-
:param cache: actual memcached cache.
781-
:param prefix: post key-encoding prepended prefix.
798+
Parameters:
782799
783-
.. code-block:: python
800+
- cache: actual memcached cache.
801+
- prefix: post key-encoding prepended prefix.
784802
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+
```
789809
"""
790810

791811
def __init__(self, cache, prefix: str = ""):
@@ -800,20 +820,22 @@ def _key(self, key):
800820
# REDIS
801821
#
802822
class RedisCache(MutableMapping):
803-
"""Redis TTL-ed wrapper for cachetools (``redis``).
823+
"""Redis TTL-ed wrapper for cachetools (`redis`).
804824
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:
808826
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*.
810830
811-
.. code-block:: python
831+
Keys and values are serialized in *JSON*.
812832
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+
```
817839
"""
818840

819841
def __init__(self, cache, ttl=600, raw=False):
@@ -893,16 +915,18 @@ def hits(self) -> float:
893915
class PrefixedRedisCache(RedisCache):
894916
"""Prefixed Redis wrapper class for cachetools.
895917
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:
899919
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_.
901923
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+
```
906930
"""
907931

908932
def __init__(self, cache, prefix: str = "", ttl=600):

0 commit comments

Comments
 (0)