
Redis cache for Python
- Simple python redis cache library, mostly used for distributed caching, where applications is running on separated processes such as Gunicorn workers, K8s replicas, Cloud, ...
- Asyncio Support for FastAPI, Starlette
Requirements
Installation
$ pip install redis-cache-py
Simple usage
from redis_cache_py import RedisCache
redis_cache = RedisCache(
host="127.0.0.1",
port=6379,
)
@redis_cache.cache(ttl=10)
def concate_list(a: list, b: list):
print("This function is called")
return a + b
result = concate_list([1, 2, 3], [4, 5, 6])
print(result)
result = concate_list([1, 2, 3], [4, 5, 6])
print(result)
Asynchronous with asyncio
import asyncio
from redis_cache_py.asyncio import AsyncRedisCache
redis_cache = AsyncRedisCache(
host="127.0.0.1",
port=6379,
verbose=1
)
@redis_cache.aio_cache(ttl=10)
async def concate_list(a: list, b: list):
print("This function is called")
return a + b
async def test_async_cache():
result = await concate_list([1, 2, 3], [4, 5, 6])
print(result)
result2 = await concate_list([1, 2, 3], [4, 5, 6])
print(result2)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_async_cache())
Advanced usage
for further examples and use cases please visit examples
Testing
NOTE: Please make sure you have redis running on 127.0.0.1:6379
to run test.
$ python3 -m unittest discover tests