Skip to main content

cachelper

https://travis-ci.org/suzaku/cachelper.svg?branch=master https://img.shields.io/pypi/v/cachelper.svg

Useful cache helpers in one package!

Sponsor

Install

pip install cachelper

Helpers

In memory cache

memoize

Caching function return values in memory.

import cachelper

@cachelper.memoize()
def fibo(n):
    if n in (0, 1):
        return 1
    return fibo(n - 1) + fibo(n - 2)

fibo(10)

Cache with Redis/Memcached

Using the HelperMixin

HelperMixin can be used with any cache client classes that implement the following methods:

  • def get(self, key)

  • def set(self, key, value, timeout=None)

  • def delete(self, key)

  • def get_many(self, keys)

  • def set_many(self, mapping, timeout=None)

For example, RedisCache from werkzeug is one such class:

from redis import StrictRedis
from werkzeug.contrib.cache import RedisCache as _RedisCache

from cachelper import HelperMixin

class RedisCache(_RedisCache, HelperMixin):
    '''werkzeug.contrib.cache.RedisCache mixed with HelperMixin'''

    def get_many(self, keys):
        return super().get_many(*keys)

rds = StrictRedis()
cache = RedisCache(rds)

This mixin defines these methods: call, map, __call__. If your class already defines methods of the same name, the mixin methods may not work correctly.

cache decorator

Add cache by decorating a function or method.

@cache("key-{user_id}", timeout=300)
def get_name(user_id):
    # Fetch user name from database
    ...

The cache key template can also be a function which acts as a key factory:

def name_key(user_id):
    return "key-%s" % user_id

@cache(name_key, timeout=300)
def get_name(user_id):
    # Fetch user name from database
    ...

Just make sure the key factory function accepts the same parameters as the cached function and returns the key.

cached function calls

Sometimes we don’t want to cache all calls to a specific function. So the decorator is not suitable, we may cache the call instead the function in this case:

def get_name(user_id):
    # Fetch user name from database
    ...

user_id = 42
key = "key-{user_id}".format(user_id=user_id)
cache.call(lambda: get_name(user_id), key, timeout=300)

cached multiple calls

For most cache backends, it’s much faster to get or set caches in bulk.

def get_name(user_id):
    # Fetch user name from database
    ...

user_ids = [1, 2, 42, 1984]
names = cache.map("key-{user_id}", get_name, user_ids, timeout=300)

Metadata

Release files for cachelper 0.3.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 cachelper 0.3.0
File Size Uploaded Provenance Actions
cachelper-0.3.0.tar.gz 3.8 kB Without provenance Details for cachelper-0.3.0.tar.gz

Release files / cachelper-0.3.0.tar.gz

Download URL cachelper-0.3.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b61f1a1e55f3752cb9bd29512276b12483b37e043f55f719b868ac4a27014e67
BLAKE2b-256 checksum
How to use checksums
6ccc75392124b0df3c76708011240c7f70c27ab510b883f3d538c277f489fe2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for cachelper 0.3.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.

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