Skip to content

Commit 20314e2

Browse files
📦️v0.1.3 - Cached Sessions
1 parent 5b088d4 commit 20314e2

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ INFO: :: Average Response Time :: 0.97 ms
9292
```
9393

9494

95+
* **Cached Sessions**: Now use cached sessions along with context manager instead of `get_db`.
96+
97+
```
98+
from fastapi import FastAPI
99+
from .db import Base, engine
100+
from fastapi_utilities import FastAPISessionMaker, repeat_every
101+
from .models import User
102+
import random
103+
104+
app = FastAPI()
105+
Base.metadata.create_all(bind=engine)
106+
107+
session_maker = FastAPISessionMaker("sqlite:///db.sqlite3")
108+
109+
110+
@app.on_event("startup")
111+
@repeat_every(seconds=5, raise_exceptions=True)
112+
async def startup():
113+
print("Starting up...")
114+
with session_maker.context_session() as session:
115+
x = User(id=random.randint(0, 10000))
116+
session.add(x)
117+
print("Startup complete!")
118+
119+
```
120+
95121

96122
---
97123

docs/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ INFO: :: Average Response Time :: 0.97 ms
9292
```
9393

9494

95+
* **Cached Sessions**: Now use cached sessions along with context manager instead of `get_db`.
96+
97+
```
98+
from fastapi import FastAPI
99+
from .db import Base, engine
100+
from fastapi_utilities import FastAPISessionMaker, repeat_every
101+
from .models import User
102+
import random
103+
104+
app = FastAPI()
105+
Base.metadata.create_all(bind=engine)
106+
107+
session_maker = FastAPISessionMaker("sqlite:///db.sqlite3")
108+
109+
110+
@app.on_event("startup")
111+
@repeat_every(seconds=5, raise_exceptions=True)
112+
async def startup():
113+
print("Starting up...")
114+
with session_maker.context_session() as session:
115+
x = User(id=random.randint(0, 10000))
116+
session.add(x)
117+
print("Startup complete!")
118+
119+
```
120+
95121

96122
---
97123

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-utilities"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
description = "Reusable utilities for FastAPI"
55
authors = ["Priyanshu Panwar <[email protected]>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)