A redis-based session backend for Fastapi apps
Project description
fastapi-redis-session
A redis-based session backend for Fastapi apps
Install
pip install -U fastapi-redis-session
Use
from typing import Any
from fastapi import Depends, FastAPI, Request, Response
from fastapi_redis_session import deleteSession, getSession, getSessionId, getSessionStorage, setSession, SessionStorage
app = FastAPI(title=__name__)
@app.post("/setSession")
async def _setSession(
request: Request, response: Response, sessionStorage: SessionStorage = Depends(getSessionStorage)
):
sessionData = await request.json()
setSession(response, sessionData, sessionStorage)
@app.get("/getSession")
async def _setSession(session: Any = Depends(getSession)):
return session
@app.post("/deleteSession")
async def _deleteSession(
sessionId: str = Depends(getSessionId), sessionStorage: SessionStorage = Depends(getSessionStorage)
):
deleteSession(sessionId, sessionStorage)
return None
Config
Deafult Config
- url of Redis: redis://localhost:6379/0
- name of sessionId: ssid
- generator function of sessionId:
lambda :uuid.uuid4().hex
- expire time of session in redis: 6 hours
Custom Config
from fastapi_redis_session.config import basicConfig
basicConfig(
redisURL="redis://localhost:6379/1",
sessionIdName="sessionId",
sessionIdGenerator=lambda: str(random.randint(1000, 9999)),
expireTime=timedelta(days=1),
)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi-redis-session-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13a43f1c2b0f4a207aae7946aa6bb3e5bf22a298a2e95cea06ec8c4a6d3a8f8d |
|
MD5 | e257bcc1d563455b295ae41181ffb97d |
|
BLAKE2b-256 | ba54dc2315f995e75ddef8a8069838205402f17ac751b32b833a58e8075b091c |
Close
Hashes for fastapi_redis_session-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a1a3460fc4909dc5b2aca8b0f7dbf5e29aca8c3d28d1ea603c45ee3f90baf1a |
|
MD5 | c41dfafd5461b23949f704cb911ea580 |
|
BLAKE2b-256 | 4b45bdb959167b8e182944328a384890b884b5b6727ecd5783a3e27136726a6b |