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.1.2.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.1.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-redis-cache-1.1.2.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.2 CPython/3.7.11

File hashes

Hashes for python-redis-cache-1.1.2.tar.gz
Algorithm Hash digest
SHA256 64494c75833dd83aaf84cc83f4e4ff9d8bf8be92f77721fbb3da6ef42412a344
MD5 49e3b321efa804cb9375c68c2fe1ea34
BLAKE2b-256 c4e760a1e3077b28699192ff416047dc88c40dbbf32a8c58df8ed3da261b5042

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_redis_cache-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.8 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.2 CPython/3.7.11

File hashes

Hashes for python_redis_cache-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d0afd72ace26d1703caa4bc2026f5fe98512e803c2c4eacab8b073a132cc5dbf
MD5 803b14c9721f3647c821f6072a6c2b23
BLAKE2b-256 7ea3d014b98d8fb0fe01389c3567db496356c11f59a6d8bbaef6e4264a18c917

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