Skip to main content

Python cache decorator and other itils with support for Redis or KeyDB

Project description

redis-cached

Python cache decorator that uses Redis or KeyDB as storage. This is very handy for replicated apps (e.g. Kubernetes), AWS Lambda functions, and other stateless apps.

Features:

  • Function result and kwarg values are pickled, so you can work with complex structures like pydantic's BaseModel
  • Prevents multiple simultaneous cache updates when a function is called concurrently and there is no cached value.
  • Cache invalidation is available

Limitations:

  • Only async functions are supported.
  • Only keyword arguments are supported. It will raise an error if you pass non-kwargs while calling your function.

Installation

pip install redis_cached

Usage

Basic usage:

import asyncio
from redis_cached import cached, invalidate_cache

@cached(5)
async def add_one(x):
    return x + 1

async def main():
    result = await add_one(x=2)  # result is cached for 5 seconds
    
    # Pass the same kwargs to this func to invalidate the cache
    await invalidate_cache('add_one', x=2)

asyncio.run(main())

Optionally, add salt to the decorator to avoid clashing with the same-named functions in other modules or other apps that use the same Redis or KeyDB database:

import asyncio
from redis_cached import cached, invalidate_cache

CACHE_SALT = 'XnsJ-7C9PIU0qhDwh9YhJQ'

@cached(5, cache_key_salt=CACHE_SALT)
async def add_one(x):
    return x + 1

async def invalidate_add_one(**kwargs):
    await invalidate_cache('add_one', cache_key_salt=CACHE_SALT, **kwargs)

async def main():
    result = await add_one(x=2)  # cached
    await invalidate_add_one(x=2)  # invalidated

asyncio.run(main())

Contributing

Contributions are welcome. Please refer to maintenance readme for more details.

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

redis_cached-0.2.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

redis_cached-0.2.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file redis_cached-0.2.1.tar.gz.

File metadata

  • Download URL: redis_cached-0.2.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for redis_cached-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c2c43827ecc0f80ac32b3503f0cb7716e8fc9d572a27a1ab32f4bc9188c565cb
MD5 f9618aadcc895f23a845b45cc1d03514
BLAKE2b-256 3105077cb89e590c01cea045c003cf72667f0a058e7a06125337d92f4dbb2a37

See more details on using hashes here.

File details

Details for the file redis_cached-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: redis_cached-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.2.0

File hashes

Hashes for redis_cached-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1018d3a8a387fec430501f141957879d1e3eab99d96ee24a5de481a9b0917a8e
MD5 b286e0562683ce7546c6af8b167dd911
BLAKE2b-256 a4cd2dd817713fede44545ef52d44baa336ffbfb5f3cc05f1c8cd4b2cceae483

See more details on using hashes here.

Supported by

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