Cache for FastAPI
Project description
fastapi-cache
Introduction
fastapi-cache
is a tool to cache fastapi response and function result, with backends support redis
and memcache
.
Features
- Support
redis
andmemcache
. - Easily integration with
fastapi
. - Support http cache like
ETag
andCache-Control
.
Requirements
asyncio
environment.redis
if useRedisBackend
.memcache
if useMemcacheBackend
.
Install
> pip install fastapi-cache2[redis]
or
> pip install fastapi-cache2[memcache]
Usage
Quick Start
import aioredis
import uvicorn
from fastapi import FastAPI
from starlette.requests import Request
from starlette.responses import Response
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from fastapi_cache.decorator import cache_response, cache
app = FastAPI()
@cache()
async def get_cache():
return 1
@app.get("/")
@cache_response(expire=60)
async def index(request: Request, response: Response):
return dict(hello="world")
@app.on_event("startup")
async def startup():
redis = await aioredis.create_redis_pool("redis://localhost", encoding="utf8")
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
Use cache_response
If you want cache fastapi
response transparently, you can use cache_response as decorator between router decorator and view function and must pass request
as param of view function.
And if you want use ETag
and Cache-Control
features, you must pass response
param also.
Use cache
You can use cache
as decorator like other cache tools to cache common function result.
License
This project is licensed under the Apache-2.0 License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastapi-cache2-0.1.0.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for fastapi_cache2-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0da8e8f7f45cd6a6591a64d6b782b9ddbd16dca6f9a45d3d674c84e0d44a7343 |
|
MD5 | aee5a28f25dfe09f0bed9e9027cec2fe |
|
BLAKE2b-256 | 1db2d2231557b7dbaa09c3079bc1d094da3a2ae833d2e7139d88ab01b092a21f |