mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge branch 'main' into patch-1
This commit is contained in:
16
README.md
16
README.md
@@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache
|
|||||||
|
|
||||||
from redis import asyncio as aioredis
|
from redis import asyncio as aioredis
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||||
|
redis = aioredis.from_url("redis://localhost")
|
||||||
|
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
@cache()
|
@cache()
|
||||||
@@ -76,12 +84,6 @@ async def get_cache():
|
|||||||
@cache(expire=60)
|
@cache(expire=60)
|
||||||
async def index():
|
async def index():
|
||||||
return dict(hello="world")
|
return dict(hello="world")
|
||||||
|
|
||||||
@asynccontextmanager
|
|
||||||
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
||||||
redis = aioredis.from_url("redis://localhost")
|
|
||||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
|
||||||
yield
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Initialization
|
### Initialization
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# pyright: reportGeneralTypeIssues=false
|
# pyright: reportGeneralTypeIssues=false
|
||||||
from typing import Dict, Optional
|
from contextlib import asynccontextmanager
|
||||||
|
from typing import AsyncIterator, Dict, Optional
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import uvicorn
|
import uvicorn
|
||||||
@@ -11,7 +12,13 @@ from pydantic import BaseModel
|
|||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import JSONResponse, Response
|
from starlette.responses import JSONResponse, Response
|
||||||
|
|
||||||
app = FastAPI()
|
@asynccontextmanager
|
||||||
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||||
|
FastAPICache.init(InMemoryBackend())
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
ret = 0
|
ret = 0
|
||||||
|
|
||||||
@@ -119,10 +126,5 @@ def namespaced_injection(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup():
|
|
||||||
FastAPICache.init(InMemoryBackend())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run("main:app", reload=True)
|
uvicorn.run("main:app", reload=True)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# pyright: reportGeneralTypeIssues=false
|
# pyright: reportGeneralTypeIssues=false
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
import time
|
import time
|
||||||
|
from typing import AsyncIterator
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
import uvicorn
|
import uvicorn
|
||||||
@@ -17,7 +19,15 @@ from starlette.responses import JSONResponse, Response
|
|||||||
import redis.asyncio as redis
|
import redis.asyncio as redis
|
||||||
from redis.asyncio.connection import ConnectionPool
|
from redis.asyncio.connection import ConnectionPool
|
||||||
|
|
||||||
app = FastAPI()
|
@asynccontextmanager
|
||||||
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
||||||
|
pool = ConnectionPool.from_url(url="redis://redis")
|
||||||
|
r = redis.Redis(connection_pool=pool)
|
||||||
|
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
app.mount(
|
app.mount(
|
||||||
path="/static",
|
path="/static",
|
||||||
@@ -80,12 +90,5 @@ async def cache_response_obj():
|
|||||||
return JSONResponse({"a": 1})
|
return JSONResponse({"a": 1})
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup():
|
|
||||||
pool = ConnectionPool.from_url(url="redis://redis")
|
|
||||||
r = redis.Redis(connection_pool=pool)
|
|
||||||
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
uvicorn.run("main:app", reload=True)
|
uvicorn.run("main:app", reload=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user