Skip to main content

Basic Redis caching for functions

Project description

CI pypi versions license

python-redis-cache

Simple redis cache for Python functions

Requirements

  • Redis 5+
  • Python 3.6+

How to install

pip install python-redis-cache

How to use

from redis import StrictRedis
from redis_cache import RedisCache

client = StrictRedis(host="redis", decode_responses=True)
cache = RedisCache(redis_client=client)


@cache.cache()
def my_func(arg1, arg2):
    result = some_expensive_operation()
    return result

# Use the function
my_func(1, 2)

# Call it again with the same arguments and it will use cache
my_func(1, 2)

# Invalidate a single value
my_func.invalidate(1, 2)

# Invalidate all values for function
my_func.invalidate_all()

Limitations and things to know

Arguments and return types must be JSON serializable by default. You can override the serializer, but be careful with using Pickle. Make sure you understand the security risks. Pickle should not be used with untrusted values. https://security.stackexchange.com/questions/183966/safely-load-a-pickle-file

  • ttl - is based on the time from when it's first inserted in the cache, not based on the last access
  • limit - The limit will revoke keys (once it hits the limit) based on FIFO, not based on LRU

API

RedisCache(redis_client, prefix="rc", serializer=dumps, deserializer=loads)

RedisCache.cache(ttl=None, limit=None, namespace=None)

# Cached function API

# Returns a cached value, if it exists in cache. Saves value in cache if it doesn't exist
cached_func(*args, *kwargs)

# Invalidates a single value
cached_func.invalidate(*args, **kwargs)

# Invalidates all values for cached function
cached_func.invalidate_all()
  • prefix - The string to prefix the redis keys with
  • serializer/deserializer - functions to convert arguments and return value to a string (user JSON by default)
  • ttl - The time in seconds to cache the return value
  • namespace - The string namespace of the cache. This is useful for allowing multiple functions to use the same cache. By default its f'{function.__module__}.{function.__file__}'

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

python-redis-cache-1.2.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_redis_cache-1.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file python-redis-cache-1.2.0.tar.gz.

File metadata

  • Download URL: python-redis-cache-1.2.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for python-redis-cache-1.2.0.tar.gz
Algorithm Hash digest
SHA256 8b592ce1ffbd3cf51958c70dc0d86d308415759914f230e058f5e1a94f79ca4b
MD5 5635096bc6e098ef6a5e03526ea4eddf
BLAKE2b-256 ef8227f6bd811f5b4566ed3ba67d2577a161515428b632c985de7a072f629b02

See more details on using hashes here.

File details

Details for the file python_redis_cache-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: python_redis_cache-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

File hashes

Hashes for python_redis_cache-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3fee1031d227987fd86457127c9e5c5669004e262ce8eeae8e8a88a8e64f5a31
MD5 69170ec6927e3110b747b817545599dc
BLAKE2b-256 ba64d49c458b2e59b2918c06aade79dc3aac2ef8df20e42599c85db8570d0e59

See more details on using hashes here.

Supported by

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