Skip to main content

CacheToolsUtils

Stackable cache classes for sharing, encryption, statistics and more on top of cachetools, redis and memcached.

Status Tests Coverage Issues Python Version Badges License

Module Contents

For our purpose, a cache is a key-value store, aka a dictionary, possibly with some constraints on keys (type, size) and values (size, serialization). This module provides new caches, wrappers and other utilities suitable to use with cachetools. Example:

import redis
import CacheToolsUtils as ctu
import config

redis_storage = redis.Redis(**config.REDIS_SERVER)
storage = ctu.RedisCache(redis_storage, raw=True, ttl=120)
crypto = ctu.EncryptedCache(storage, config.CACHE_KEY)
cache = ctu.ToBytesCache(crypto)

@ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
def repeat(s: str, n: int) -> str:
    return s * n

@ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
def banged(s: str, n: int) -> str:
    return repeat(s, n) + "!"

@ctu.cached(ctu.AutoPrefixedCache(cache), key=ctu.json_key)
def question(s: str, n: int) -> str:
    return repeat(s, n) + "?"

print(banged("aa", 3))    # add 2 cache entries
print(question("aa", 3))  # add 1 entry, 1 hit
print(repeat("aa", 3))    # cache hit!
print(banged("aa", 3))    # cache hit!

assert cache.hits() > 0

Cache classes

  • RedisCache allows to see a Redis server as a python cache by wrapping a redis.Redis instance.
  • MemCached does the same for a Memcached server. The utility class JsonSerde is a convenient JSON serializer-deserializer class for Memcached.
  • DictCache a very simple dict cache.

Wrappers to extend cache capabilities

  • PrefixedCache, PrefixedMemCached and PrefixedRedisCache add a prefix to distinguish sources on a shared cache.
  • AutoPrefixedCache add a counter-based prefix.
  • StatsCache, MemCached and RedisCache add a hits method to report the cache hit rate, stats to report statistics and reset to reset statistics.
  • LockedCache use a (thread) lock to control cache accesses.
  • TwoLevelCache allows to combine two caches.
  • DebugCache to trace cache calls using logging.
  • EncryptedCache a cache with key hashing and value encryption.
  • ToBytesCache map keys and values to bytes.
  • BytesCache map bytes keys and values to strings.

Cache utilities

  • cached decorator: a cachetools replacement which allows to test if a function result is in cache, and to delete such an entry.
  • cacheFunctions and cacheMethods: add caching to functions or methods.
  • json_key, hash_json_key, full_hash_key: convenient JSON-based cache key serialization functions for cached.

License

This code is Public Domain.

All software has bug, this is software, hence… Beware that you may lose your hairs or your friends because of it. If you like it, feel free to send a postcard to the author.

More

See the documentation, sources and issues on GitHub.

See packages on PyPI.

Metadata

Release files for CacheToolsUtils 12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for CacheToolsUtils 12.0
File Size Uploaded Provenance Actions
cachetoolsutils-12.0.tar.gz 12.1 kB Without provenance Details for cachetoolsutils-12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for CacheToolsUtils 12.0
File Interpreter ABI Platform Actions
cachetoolsutils-12.0-py3-none-any.whl Python 3 none any Details for cachetoolsutils-12.0-py3-none-any.whl

Total release size: 22.3 kB

Release files / cachetoolsutils-12.0.tar.gz

Download URL cachetoolsutils-12.0.tar.gz
Size 12.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2e07522742a17faed6c0e7c05a6736184c53bad3c7030e1d1a552bf7b75a42c4
BLAKE2b-256 checksum
How to use checksums
adf1cd1a302d494c29d8eff83ee20c8d6822d779b9bfe468a601fe7568b30637
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / cachetoolsutils-12.0-py3-none-any.whl

Download URL cachetoolsutils-12.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d98d38e037782575bcf1511880968dfdf3ce799dc044dea7ef505cf2d852d951
BLAKE2b-256 checksum
How to use checksums
e6c040b1afb31ad5600b91dea97adc4bebd72a6fcf2fea745235c9c2af59a5c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for CacheToolsUtils 12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page