FastAPI simple cache
Project description
FastAPI Cache
Implements simple lightweight cache system as dependencies in FastAPI.
Installation
pip install fastapi-cache
Usage example
from fastapi import Depends, FastAPI
from fastapi_cache import caches, close_caches
from fastapi_cache.backends.redis import CACHE_KEY, RedisCacheBackend
app = FastAPI()
def redis_cache():
return caches.get(CACHE_KEY)
@app.get('/')
async def hello(
cache: RedisCacheBackend = Depends(redis_cache)
):
in_cache = await cache.get('some_cached_key')
if not in_cache:
await cache.set('some_cached_key', 'new_value', 5)
return {'response': in_cache or 'default'}
@app.on_event('startup')
async def on_startup() -> None:
rc = RedisCacheBackend('redis://redis')
caches.set(CACHE_KEY, rc)
@app.on_event('shutdown')
async def on_shutdown() -> None:
await close_caches()
TODO
- Add tests
- Add registry decorator
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
fastapi-cache-0.0.3.tar.gz
(3.5 kB
view hashes)
Built Distribution
Close
Hashes for fastapi_cache-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99d9cb90c154d2cc31495596ce6b5d3f4a4883c9ab69e207df5b79ffa5220974 |
|
MD5 | aadb655860594ec7828152b2ab568c2d |
|
BLAKE2b-256 | 045e110bb6259a6eab470d69ae316333154d7a9cb2edf368140b4fcff182934b |