Metadata-Version: 2.4
Name: fastapi-redis-session
Version: 0.3.0
Summary: A redis-based session backend for Fastapi apps
Author-email: duyixian <duyixian1234@qq.com>
License-Expression: MIT
Project-URL: Repository, https://github.com/duyixian1234/fastapi-redis-session
Requires-Python: <4.0,>=3.12
Description-Content-Type: text/markdown
Requires-Dist: fastapi<0.71.0,>=0.70.0
Requires-Dist: redis<7.0.0,>=6.0.0

# fastapi-redis-session

![CI](https://github.com/duyixian1234/fastapi-redis-session/workflows/CI/badge.svg?branch=master)

A redis-based session backend for Fastapi apps

## Install

```bash
pip install -U fastapi-redis-session
```

Requires Python 3.12 or newer.

The public API is async-only and uses `redis.asyncio`.

## Development

```bash
uv sync --group dev
uv run pytest
```

## Use

```python
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()
    await 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)
):
    await deleteSession(sessionId, sessionStorage)
    return None

```

`getSession` is an async dependency, and `setSession` / `deleteSession` must be awaited inside async route handlers.

## Migration notes

- `SessionStorage` now uses `redis.asyncio.Redis`.
- Session access is no longer exposed through sync magic methods; use the async helper functions or awaitable storage methods instead.
- Existing code that called `setSession(...)` or `deleteSession(...)` without `await` must be updated.

## 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

```python
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),
    )
```
