Skip to content

Commit

Permalink
Merge pull request #433 from nikstuckenbrock/feature/update-project-docs
Browse files Browse the repository at this point in the history
Update project docs and examples
  • Loading branch information
long2ice authored Jul 24, 2024
2 parents 865dba1 + 552d54c commit 00d34e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ from fastapi_cache.decorator import cache

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()
Expand All @@ -76,12 +84,6 @@ async def get_cache():
@cache(expire=60)
async def index():
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
Expand Down
16 changes: 9 additions & 7 deletions examples/in_memory/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pyright: reportGeneralTypeIssues=false
from typing import Dict, Optional
from contextlib import asynccontextmanager
from typing import AsyncIterator, Dict, Optional

import pendulum
import uvicorn
Expand All @@ -11,7 +12,13 @@
from starlette.requests import Request
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

Expand Down Expand Up @@ -119,10 +126,5 @@ def namespaced_injection(
}


@app.on_event("startup")
async def startup():
FastAPICache.init(InMemoryBackend())


if __name__ == "__main__":
uvicorn.run("main:app", reload=True)
19 changes: 11 additions & 8 deletions examples/redis/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pyright: reportGeneralTypeIssues=false
from contextlib import asynccontextmanager
import time
from typing import AsyncIterator

import pendulum
import uvicorn
Expand All @@ -17,7 +19,15 @@
import redis.asyncio as redis
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(
path="/static",
Expand Down Expand Up @@ -80,12 +90,5 @@ async def cache_response_obj():
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__":
uvicorn.run("main:app", reload=True)

0 comments on commit 00d34e4

Please sign in to comment.