mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Merge branch 'master' into documentation
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from datetime import date, datetime
|
||||
import time
|
||||
|
||||
import aioredis
|
||||
import redis.asyncio as redis
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from redis.asyncio.connection import ConnectionPool
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
|
||||
@@ -39,6 +41,13 @@ async def get_data(request: Request, response: Response):
|
||||
return date.today()
|
||||
|
||||
|
||||
@app.get("/blocking")
|
||||
@cache(namespace="test", expire=20)
|
||||
def blocking(request: Request, response: Response):
|
||||
time.sleep(5)
|
||||
return dict(ret=get_ret())
|
||||
|
||||
|
||||
@app.get("/datetime")
|
||||
@cache(namespace="test", expire=20)
|
||||
async def get_datetime(request: Request, response: Response):
|
||||
@@ -47,8 +56,9 @@ async def get_datetime(request: Request, response: Response):
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
redis = aioredis.from_url(url="redis://localhost")
|
||||
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
|
||||
pool = ConnectionPool.from_url(url="redis://localhost")
|
||||
r = redis.Redis(connection_pool=pool)
|
||||
FastAPICache.init(RedisBackend(r), prefix="fastapi-cache")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user