File tree Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11[tool .poetry ]
22name = " fastapi-utilities"
3- version = " 0.1.2 "
3+ version = " 0.1.3 "
44description = " Reusable utilities for FastAPI"
55authors = [
" Priyanshu Panwar <[email protected] >" ]
66readme = " README.md"
You can’t perform that action at this time.
0 commit comments