Skip to main content

Cachetools Utilities

Project description

cachetools-utils

Classes to add key prefix and stats to cachetools classes and use redis and memcached as storage backends, and other utils.

Install

Install with pip:

pip install CacheToolsUtils

See below for example usage.

Documentation

This module provide the following cache wrappers suitable to use with cachetools:

PrefixedCache

Add a key prefix.

import CacheToolsUtils as ctu

ct_base = cachetools.TTLCache(maxsize=1048576, ttl=600)
foo_cache = ctu.PrefixedCache(ct_base, "foo.")
bla_cache = ctu.PrefixedCache(ct_base, "bla.")

@cachetools.cached(cache=foo_cache)
def foo():
    return 

@cachetools.cached(cache=bla_cache)
def bla():
    return 

StatsCache

Keep stats, cache hit rate shown with hits().

scache = StatsCache(cache)

TwoLevelCache

Two-level cache, for instance a local in-memory cachetools cache for the first level, and a larger shared redis or memcached distributed cache for the second level.

cache = TwoLevelCache(TTLCache(), RedisCache())

MemCached

Basic wrapper with JSON key encoding.

import pymemcache as pmc

mc_base = pmc.Client(server="localhost", serde=ctu.JsonSerde())
cache = ctu.MemCached(mc_base)

@cachetools.cached(cache=cache)
def poc():

PrefixedMemCached

Wrapper with a prefix.

pcache = ctu.PrefixedMemCached(mc_base, prefix="pic.")

StatsMemCached

Wrapper with stats actually taken from the MemCached server.

scache = ctu.StatsMemCached(mc_base)

RedisCache

TTL'ed Redis wrapper, default ttl is 10 minutes.

import redis

rd_base = redis.Redis(host="localhost")
cache = ctu.RedisCache(rd_base, ttl=60)

PrefixedRedisCache

Wrapper with a prefix and a ttl.

pcache = ctu.PrefixedRedisCache(rd_base, "pac.", ttl=3600)

StatsRedisCache

Wrapper with stats (call hits()) and a ttl.

scache = ctu.StatsRedisCache(pcache)

License

This code is public domain.

Versions

1.1.0 on 2022-01-30

Improved documentation. Add TwoLevelCache. Add 100% coverage test.

1.0.0 on 2022-01-29

Add set, get and delete forwarding to RedisCache, so that redis classes can be stacked.

0.9.0 on 2022-01-29

Initial version extracted from another project.

TODO

  • improve documentation further.
  • add a close?

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-1.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

CacheToolsUtils-1.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file CacheToolsUtils-1.1.0.tar.gz.

File metadata

  • Download URL: CacheToolsUtils-1.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for CacheToolsUtils-1.1.0.tar.gz
Algorithm Hash digest
SHA256 ccede5ad4c26c92220ac7ec64825161e84bd90b6d3e9240efafd36101bbe1775
MD5 cac1ec9717fcd00b84046a11c47b2e24
BLAKE2b-256 e2cf477628575e058bd0a65cde19d079a2d0778592466c9b7fe2ba9188422b37

See more details on using hashes here.

File details

Details for the file CacheToolsUtils-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: CacheToolsUtils-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for CacheToolsUtils-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ac0268e175e933e11896c7bf9686c92094fed47904278662c378f2eb9ecd2c1
MD5 3a36cddf6ea343fccd75ca113db6e550
BLAKE2b-256 c544bd289882893d91e23662998c7038d4015a0cb2c10984e1d1556177a28132

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