From 20314e261822dfdad1d9466b9329290fa9f5fa84 Mon Sep 17 00:00:00 2001 From: Priyanshu Panwar Date: Sun, 8 Oct 2023 10:20:36 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=EF=B8=8Fv0.1.3=20-=20Cached=20Sess?= =?UTF-8?q?ions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++++++++++++++++++++++ docs/index.md | 26 ++++++++++++++++++++++++++ pyproject.toml | 2 +- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4447332..4c1d1e1 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,32 @@ INFO: :: Average Response Time :: 0.97 ms ``` +* **Cached Sessions**: Now use cached sessions along with context manager instead of `get_db`. + +``` +from fastapi import FastAPI +from .db import Base, engine +from fastapi_utilities import FastAPISessionMaker, repeat_every +from .models import User +import random + +app = FastAPI() +Base.metadata.create_all(bind=engine) + +session_maker = FastAPISessionMaker("sqlite:///db.sqlite3") + + +@app.on_event("startup") +@repeat_every(seconds=5, raise_exceptions=True) +async def startup(): + print("Starting up...") + with session_maker.context_session() as session: + x = User(id=random.randint(0, 10000)) + session.add(x) + print("Startup complete!") + +``` + --- diff --git a/docs/index.md b/docs/index.md index 4447332..4c1d1e1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -92,6 +92,32 @@ INFO: :: Average Response Time :: 0.97 ms ``` +* **Cached Sessions**: Now use cached sessions along with context manager instead of `get_db`. + +``` +from fastapi import FastAPI +from .db import Base, engine +from fastapi_utilities import FastAPISessionMaker, repeat_every +from .models import User +import random + +app = FastAPI() +Base.metadata.create_all(bind=engine) + +session_maker = FastAPISessionMaker("sqlite:///db.sqlite3") + + +@app.on_event("startup") +@repeat_every(seconds=5, raise_exceptions=True) +async def startup(): + print("Starting up...") + with session_maker.context_session() as session: + x = User(id=random.randint(0, 10000)) + session.add(x) + print("Startup complete!") + +``` + --- diff --git a/pyproject.toml b/pyproject.toml index f56179b..75e7c0a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fastapi-utilities" -version = "0.1.2" +version = "0.1.3" description = "Reusable utilities for FastAPI" authors = ["Priyanshu Panwar "] readme = "README.md"