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
- Redis 5+
- Python 3.6+
Installation
$ pip install redis-cache-py
Simple usage
from redis_cache_py import RedisCache
# init redis_cache instance and connection
# make sure you have redis running on `127.0.0.1:6379`
redis_cache = RedisCache(
host="127.0.0.1",
port=6379,
)
@redis_cache.cache(ttl=10) # Expire after 10 seconds
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)
# This function is called
# [1, 2, 3, 4, 5, 6]
# Now result is cached, next time you call this function, result will returned
# from redis
result = concate_list([1, 2, 3], [4, 5, 6])
print(result)
# [1, 2, 3, 4, 5, 6]
Asynchronous with asyncio
import asyncio
from redis_cache_py.asyncio import AsyncRedisCache
# init redis_cache instance and connection
# Make sure you have redis running on `127.0.0.1:6379`
redis_cache = AsyncRedisCache(
host="127.0.0.1",
port=6379,
verbose=1 # Turn on logging for demonstration, set to 0 for silent caching
)
@redis_cache.aio_cache(ttl=10) # Expire after 10 seconds
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)
# Now the result is cached
result2 = await concate_list([1, 2, 3], [4, 5, 6])
print(result2)
loop = asyncio.get_event_loop()
loop.run_until_complete(test_async_cache())
# Output:
# This function is called
# [1, 2, 3, 4, 5, 6]
# [1, 2, 3, 4, 5, 6]
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
Release files for redis-cache-py 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redis-cache-py-0.0.2.tar.gz | 7.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redis_cache_py-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.9 kB
Release files / redis-cache-py-0.0.2.tar.gz
| Download URL | redis-cache-py-0.0.2.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb0de3f4e6f7aedd53f5b56fbdd34be9c035e8c93313ab167ccd9e2bc8defc16
|
|
BLAKE2b-256 checksum How to use checksums |
2087f9504429867a83987667c56260c5e2a98ca52e969f5a2986ad7e275b33b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.8.10
|
Release files / redis_cache_py-0.0.2-py3-none-any.whl
| Download URL | redis_cache_py-0.0.2-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb32e74993fbb27e81f50e7259935b1fbc65255e6758f305c2fb584525d9faa3
|
|
BLAKE2b-256 checksum How to use checksums |
c7c899f637cadd0d9cc2e06d5cfa152e0d862c9fbc427e38b365d71dbc0fdae4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.8.10
|