Skip to main content

Cachetools Utilities

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cachetoolsutils-10.4.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

cachetoolsutils-10.4-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file cachetoolsutils-10.4.tar.gz.

File metadata

  • Download URL: cachetoolsutils-10.4.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for cachetoolsutils-10.4.tar.gz
Algorithm Hash digest
SHA256 edd29539b704a40eb2a8240ff5a0f35abcbb3e04132b4cf2001f32029d18cf2d
MD5 ba7b6908743ae965c055cea165dc326f
BLAKE2b-256 7b22acc9d8b590c9bf7646203da8fc472971274b759c13e6511c459adb4b2f3b

See more details on using hashes here.

File details

Details for the file cachetoolsutils-10.4-py3-none-any.whl.

File metadata

File hashes

Hashes for cachetoolsutils-10.4-py3-none-any.whl
Algorithm Hash digest
SHA256 93dd2b7b226a07c72816a9e7683ee88c2b5f341344e4de978e3029d31d660b23
MD5 4f566800b09f8a57a4825275765c38b9
BLAKE2b-256 95bc8daa19a83cedf66b2fe5a89c74d2d9f51df4254d4415093b55d2a8c3acc1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page