#session-cookies #tower-sessions #session-management #axum #tower

tower-sessions-file-store

Store sessions for Tower Session store on the file system

5 releases

Uses new Rust 2024

0.2.0 Mar 30, 2025
0.1.3 Nov 30, 2024
0.1.2 Nov 3, 2024
0.1.1 Nov 3, 2024
0.1.0 Nov 3, 2024

#2600 in Asynchronous

Download history 13/week @ 2025-01-08 206/week @ 2025-01-22 9/week @ 2025-01-29 9/week @ 2025-02-05 9/week @ 2025-02-19 8/week @ 2025-02-26 4/week @ 2025-03-12 122/week @ 2025-03-26 19/week @ 2025-04-02 9/week @ 2025-04-09

150 downloads per month

MIT license

9KB
135 lines

Overview

A session storage that stores each session in a seperate file in a special folder (default ".sessions")

Useful when you want something more persistant than a in memory store but don't want to setup an entire database, especially during local development. Should work fine in production environments though.

Expiry

You can enable automatically deleting expired sessions like this:

let deletion_task = tokio::task::spawn(
    session_store
    .clone()
    .continuously_delete_expired(tokio::time::Duration::from_secs(60)),
);

By default, it will only load sessions to check their expirty if the last modified date of the file is at least 60 seconds. You can adjust this with set_minimum_expiry_date. Ideally the expiry date would be the same as the duration of your sessions.


Usage

let session_store = FileSessionStorage::new();
let session_layer = SessionManagerLayer::new(session_store)
    .with_expiry(Expiry::OnInactivity(Duration::secs(60 * 60)));
let deletion_task = tokio::task::spawn(
    session_store
        .clone()
        .continuously_delete_expired(tokio::time::Duration::from_secs(60 * 60)),
);

app.layer(session_layer);

Issues or pull requests welcome.

Dependencies

~6–13MB
~146K SLoC