API Reference
CacheTools Utilities. This code is public domain.
AutoPrefixedCache
Bases: PrefixedCache
Cache class with an automatic counter-based string prefix.
Parameters:
- cache: actual cache.
- sep: prefix separator, default is ".".
- method: encoding method in "b64", "b64u", "b32", "b32x", "b16", "a85" and "b85". Default is "b64".
Source code in CacheToolsUtils.py
BytesCache
Bases: _KeyMutMapMix
, _StatsMix
, MutableMapping
Map bytes to strings.
Source code in CacheToolsUtils.py
DebugCache
Bases: _StatsMix
, MutableMapping
Debug class.
Parameters:
- cache: actual cache
- log: logging instance
- name: name of instance for output
Source code in CacheToolsUtils.py
DictCache
EncryptedCache
Bases: _KeyMutMapMix
, _StatsMix
, MutableMapping
Encrypted Bytes Key-Value Cache.
Parameters:
- secret: bytes of secret, at least 16 bytes.
- hsize: size of hashed key, default is 16.
- csize: value checksum size, default is 0.
- cipher: chose cipher from "Salsa20", "AES-128-CBC" or "ChaCha20".
The key is not encrypted but simply hashed, thus they are fixed size with a very low collision probability.
By design, the clear-text key is needed to recover the value, as each value is encrypted with its own key and nonce.
Algorithms:
- SHA3: hash/key/nonce derivation and checksum.
- Salsa20, AES-128-CBC or ChaCha20: value encryption.
Source code in CacheToolsUtils.py
JsonSerde
JSON serialize/deserialize class for MemCached (pymemcache
).
import pymemcache as pmc
import CacheToolsUtils as ctu
pmc_cache = pmc.Client(server="localhost", serde=ctu.JsonSerde())
Source code in CacheToolsUtils.py
LockedCache
Bases: _MutMapMix
, _StatsMix
, _RedisMix
, MutableMapping
Cache class with a lock.
Parameters:
- cache: actual cache.
- lock: lock (context manager) to use
The locked layer should be the last one before the actual cache.
import threading
import cachetools as ct
import CacheToolsUtils as ctu
cache = ctu.LockedCache(ct.LFUCache(), threading.RLock())
Source code in CacheToolsUtils.py
MemCached
Bases: _KeyMutMapMix
, MutableMapping
MemCached-compatible wrapper class for cachetools with key encoding.
Parameter:
- cache: actual memcached cache.
import pymemcache as pmc
import CacheToolsUtils as ctu
cache = ctu.MemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()))
@ctu.cached(cache=cache)
def whatever(...):
...
Source code in CacheToolsUtils.py
clear()
hits()
PrefixedCache
Bases: _KeyMutMapMix
, _StatsMix
, MutableMapping
Cache class to add a key prefix.
Parameters:
- cache: actual cache.
- prefix: prefix to prepend to keys.
Source code in CacheToolsUtils.py
PrefixedMemCached
Bases: MemCached
MemCached-compatible wrapper class for cachetools with a key prefix.
Parameters:
- cache: actual memcached cache.
- prefix: post key-encoding prepended prefix.
import pymemcache as pmc
import CacheToolsUtils as ctu
# add a "app." prefix to keys, after serialization
cache = ctu.PrefixedMemCached(pmc.Client(server="localhost", serde=ctu.JsonSerde()), "app.")
Source code in CacheToolsUtils.py
PrefixedRedisCache
Bases: RedisCache
Prefixed Redis wrapper class for cachetools.
Parameters:
- cache: actual redis cache.
- prefix: post key encoding prefix, default is empty.
- ttl: time-to-live in seconds, used as default expiration (
ex
), default is 600.
import redis
import CacheToolsUtils as ctu
# redis with "app." prefix and 1 hour expiration
cache = ctu.PrefixedRedisCache(redis.Redis(host="localhost"), "app.", 3600)
Source code in CacheToolsUtils.py
RedisCache
Bases: MutableMapping
Redis TTL-ed wrapper for cachetools (redis
).
Parameters:
- cache: actual redis cache.
- ttl: time-to-live in seconds, used as default expiration (
ex
), default is 600. - raw: whether to serialize keys and values, default is False.
Keys and values are serialized in JSON.
import redis
import CacheToolsUtils as ctu
# redis with 1 hour expiration
cache = ctu.RedisCache(redis.Redis(host="localhost"), 3600)
Source code in CacheToolsUtils.py
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 |
|
clear()
dbsize(*args, **kwargs)
delete(index)
get(index, default=None)
hits()
info(*args, **kwargs)
set(index, value, **kwargs)
StatsCache
Bases: _MutMapMix
, MutableMapping
Cache class to keep stats.
Parameter:
- cache: actual cache.
Note that CacheTools cached
decorator with info=True
provides
hits, misses, maxsize and currsize information.
However, this only works for its own classes.
Source code in CacheToolsUtils.py
hits()
reset()
stats()
Return available stats data as dict.
Source code in CacheToolsUtils.py
StatsRedisCache
Bases: PrefixedRedisCache
TTL-ed Redis wrapper class for cachetools.
This class is nearly empty.
Source code in CacheToolsUtils.py
ToBytesCache
Bases: _KeyMutMapMix
, _StatsMix
, MutableMapping
Map (JSON-serializable) cache keys and values to bytes.
Source code in CacheToolsUtils.py
TwoLevelCache
Bases: _MutMapMix
, MutableMapping
Two-Level Cache class for CacheTools.
Parameters:
- cache: first (smaller, shorter TTL) cache
- cache2: second (larger, longer TTL) cache
- resilient: whether to ignore cache2 failures
Source code in CacheToolsUtils.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
cacheFunctions(cache, globs, gen=PrefixedCache, opts={}, **funs)
Cache some global functions, with a prefix.
Parameters:
- cache: cache to use.
- globs: global object dictionary.
- gen: generator of PrefixedCache.
- opts: additional parameters when calling
cached
. - funs: name of functions and corresponding prefix
Source code in CacheToolsUtils.py
cacheMethods(cache, obj, gen=PrefixedCache, opts={}, **funs)
Cache some object methods.
Parameters:
- cache: cache to use.
- obj: object instance to be cached.
- gen: generator of PrefixedCache.
- opts: additional parameters when calling
cached
. - funs: name of methods and corresponding prefix
Source code in CacheToolsUtils.py
cached(cache, *args, **kwargs)
Extended decorator with delete and exists.
All parameters are forwarded to cachetools.cached
.
Parameter:
- cache: actual cache.
If f(*args, **kwargs) is the cached function, then:
f.cache_in(*args, **kwargs)
tells whether the result is cached.f.cache_del(*args, **kwargs)
deletes (invalidates) the cached result.
Source code in CacheToolsUtils.py
full_hash_key(*args, **kwargs)
Reduce arguments to a single 128-bits hash.
hash_json_key(*args, **kwargs)
json_key(*args, **kwargs)
JSON serialization of arguments.