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 Cache

cache = Cache()

@cache.cached(5)
async def get_user_data(user_id: int):
    # expensive API call here
    return {'user_id': user_id, 'name': 'John Doe'}

async def main():
    user_data = await get_user_data(user_id=1)  # result is cached for 5 seconds
    
    # To invalidate the cache for the user with ID 1, pass the same kwargs:
    await cache.invalidate_cache('get_user_data', user_id=1)

asyncio.run(main())

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

You can also use your custom-configured async Redis instance with the cache.

import asyncio
from redis.asyncio.client import Redis
from redis_cached import Cache

custom_redis = Redis(
    host='localhost',
    port=6379,
    db=0
)
cache = Cache(
    cache_key_salt='qhDwh9Y',
    redis_=custom_redis
)

@cache.cached(5)
async def get_user_data(user_id: int):
    # expensive API call here
    return {'user_id': user_id, 'name': 'John Doe'}

async def main():
    user_data = await get_user_data(user_id=1)  # cached
    await cache.invalidate_cache('get_user_data', user_id=1)  # invalidated

asyncio.run(main())

Contributing

Contributions are welcome. Please refer to the 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.4.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

redis_cached-0.4.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for redis_cached-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7253a312d80bb16400eff00a067cecf4d5e105f10a3495021625519a4fb03c2d
MD5 f4e95b7ef3f18988c789e12f3f5f457b
BLAKE2b-256 1f326da811ae2540c75d20e7774636de7c27df1a10060cff53ad99608c10bc67

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for redis_cached-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1063e2ba84cf966f76024e5ce636ad50acf07c3b303b014459087df6306eda59
MD5 768bbcfe33080624495117462537bd50
BLAKE2b-256 8daf1bb1137ecae4c895e5dec84eb3b87976bf82256569c27c099c2516008a01

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