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
Built Distribution
File details
Details for the file redis_cached-0.4.3.tar.gz
.
File metadata
- Download URL: redis_cached-0.4.3.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.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a288bf8e7eb0dfb14a9defdd0b10a95e810faeb4ab87b7d24710a6736dbf310e |
|
MD5 | 8309c5f2db64dca2ba22ed7283822535 |
|
BLAKE2b-256 | 562e64f85f5bd205d29958e4d4d97432bd4457298027023f3ab2b4877d2c4822 |
File details
Details for the file redis_cached-0.4.3-py3-none-any.whl
.
File metadata
- Download URL: redis_cached-0.4.3-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.3 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54dd07c52d81b466b4bfd3fb0c0b86fe3a6310fe05cd8bf1237a0f81452a440c |
|
MD5 | e35ab9258ff5caa16553e64f4f16c216 |
|
BLAKE2b-256 | e099a00e9d6cad5694f2be8f08919a53b3c0db4fb018db78d4db6ddd81b79217 |