Skip to main content

A redis-based session backend for Fastapi apps

Project description

fastapi-redis-session

CI

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

fastapi-redis-session-0.2.0.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

fastapi_redis_session-0.2.0-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page