mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 13:07:53 +00:00
Merge branch 'chiliec-patch-1' into integration
This commit is contained in:
42
README.md
42
README.md
@@ -50,13 +50,42 @@ or
|
|||||||
|
|
||||||
### Quick Start
|
### Quick Start
|
||||||
|
|
||||||
```python
|
Using in-memory backend:
|
||||||
from collections.abc import AsyncIterator
|
|
||||||
from contextlib import asynccontextmanager
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from fastapi_cache import FastAPICache
|
||||||
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||||
|
from fastapi_cache.decorator import cache
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
|
@cache()
|
||||||
|
async def get_cache():
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
@cache(expire=60)
|
||||||
|
async def index():
|
||||||
|
return dict(hello="world")
|
||||||
|
```
|
||||||
|
|
||||||
|
or [Redis](https://redis.io) backend:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from starlette.requests import Request
|
|
||||||
from starlette.responses import Response
|
|
||||||
|
|
||||||
from fastapi_cache import FastAPICache
|
from fastapi_cache import FastAPICache
|
||||||
from fastapi_cache.backends.redis import RedisBackend
|
from fastapi_cache.backends.redis import RedisBackend
|
||||||
@@ -66,7 +95,7 @@ from redis import asyncio as aioredis
|
|||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
async def lifespan(app: FastAPI):
|
||||||
redis = aioredis.from_url("redis://localhost")
|
redis = aioredis.from_url("redis://localhost")
|
||||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||||
yield
|
yield
|
||||||
@@ -85,7 +114,6 @@ async def get_cache():
|
|||||||
async def index():
|
async def index():
|
||||||
return dict(hello="world")
|
return dict(hello="world")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Initialization
|
### Initialization
|
||||||
|
|
||||||
First you must call `FastAPICache.init` during startup FastAPI startup; this is where you set global configuration.
|
First you must call `FastAPICache.init` during startup FastAPI startup; this is where you set global configuration.
|
||||||
|
|||||||
Reference in New Issue
Block a user