THE ORIONIS API
Build with clarity.
Explore the building blocks of an async-first Python framework. Every module, class, and method — connected, searchable, and ready to build with.
class FileSessionStore(ISessionStore): (source)
Constructor: FileSessionStore(directory)
Session store that persists each session as a JSON file on disk.
One JSON file per session, named {session_id}.json, stored inside directory. The directory is created on instantiation when it does not already exist.
Expired sessions are evicted lazily during read, keeping
hot-path operations at O(1) complexity. Bulk removal of stale
files can be triggered explicitly via gc.
File layout
{
"id": "...",
"expires_at": "2026-07-10T12:00:00+00:00",
"data": { ... }
}| Parameters | |
| directory | Path to the directory where session files are stored. |
| Static Method | _temp |
Build a unique staging path for an atomic write. |
| Method | __init__ |
Initialise the store and ensure the storage directory exists. |
| Method | _delete |
Unlink path, silently ignoring a missing-file error. |
| Method | _deserialize |
Decode raw JSON bytes into a SessionRecord. |
| Method | _gc |
Scan the store directory and remove stale or corrupt files. |
| Method | _lock |
Select a bounded, stable cross-process lock for a session file. |
| Method | _path |
Return the absolute path to the JSON file for session_id. |
| Method | _read |
Read path as raw bytes, returning None if absent. |
| Method | _read |
Load, decode and validate the record stored at path. |
| Method | _replace |
Publish tmp as path, retrying a transient sharing violation. |
| Method | _serialize |
Encode record to compact JSON bytes via msgspec. |
| Method | _update |
Replace an existing live session under its cross-process lock. |
| Method | _write |
Atomically write content to path. |
| Method | _write |
Serialise record and store it atomically at path. |
| Async Method | delete |
Remove the session file for session_id (no-op when absent). |
| Async Method | gc |
Remove all expired, corrupt, or incomplete session files. |
| Async Method | read |
Load and return the live record for session_id, or None. |
| Async Method | update |
Update only a live session without recreating a deleted file. |
| Async Method | write |
Serialise and persist record to disk atomically. |
| Class Variable | __slots__ |
Undocumented |
| Instance Variable | _directory |
Undocumented |
| Instance Variable | _locks |
Undocumented |
| Instance Variable | _rename |
Undocumented |
Decode raw JSON bytes into a SessionRecord.
Type validation is delegated entirely to msgspec; any structural mismatch or missing field is treated as corruption.
| Parameters | |
raw:bytes | Raw JSON bytes previously written by _serialize. |
| Returns | |
SessionRecord | None | Deserialised record, or None on any decode error. |
Scan the store directory and remove stale or corrupt files.
Uses os.scandir for reduced object allocation and
filesystem cache reuse. A session file is removed when it is
expired, contains invalid JSON, is structurally incomplete, or
cannot be read due to an I/O error. Staging files left behind by
an interrupted write are reclaimed once they are old enough that
no writer can still be filling them.
| Returns | |
None | Undocumented |
Select a bounded, stable cross-process lock for a session file.
| Parameters | |
path:Path | Session file protected by the lock. |
| Returns | |
BaseFileLock | Platform lock serializing validation, replacement and deletion. |
Return the absolute path to the JSON file for session_id.
| Parameters | |
sessionstr | Unique session identifier. |
| Returns | |
Path | Full path to {session_id}.json inside the store directory. |
| Raises | |
SessionStorageException | If an identifier contains path syntax or exceeds its limit. |
Read path as raw bytes, returning None if absent.
| Parameters | |
path:Path | File to read. |
| Returns | |
bytes | None | File contents, or None when the file does not exist. |
Load, decode and validate the record stored at path.
Runs entirely on a worker thread: file I/O, JSON decoding and the eviction of an expired or corrupt file happen in a single hop.
| Parameters | |
path:Path | Session file to load. |
| Returns | |
SessionRecord | None | The live record, or None when absent, expired or corrupt. |
Publish tmp as path, retrying a transient sharing violation.
The rename is serialised per store instance, and retried when the operating system refuses it because another process is swapping the same destination. POSIX never reaches the retry branch.
| Parameters | |
tmp:Path | Staging file holding the serialised payload. |
path:Path | Destination session file. |
| Returns | |
None | Undocumented |
| Raises | |
OSError | If the rename keeps failing after the last attempt. |
Encode record to compact JSON bytes via msgspec.
| Parameters | |
record:SessionRecord | Record to serialise. |
| Returns | |
bytes | UTF-8 encoded JSON payload. |
Replace an existing live session under its cross-process lock.
| Parameters | |
path:Path | Existing session file. |
record:SessionRecord | Replacement record. |
| Returns | |
bool | False when another worker deleted or expired the record. |
Atomically write content to path.
Writes to a unique .tmp sibling first, then renames over the destination to prevent partial reads from concurrent processes. A failed write removes its own staging file before propagating.
| Parameters | |
path:Path | Destination file path. |
content:bytes | Serialised session payload. |
| Returns | |
None | Undocumented |
| Raises | |
OSError | If the payload cannot be staged or renamed into place. |
Serialise record and store it atomically at path.
| Parameters | |
path:Path | Destination session file. |
record:SessionRecord | The record to persist. |
| Returns | |
None | Undocumented |
Load and return the live record for session_id, or None.
Expired or corrupt records are evicted lazily: the file is deleted and None is returned without triggering a GC cycle.
| Parameters | |
sessionstr | Unique session identifier to look up. |
| Returns | |
SessionRecord | None | The live record, or None when absent, expired, or corrupt. |
Update only a live session without recreating a deleted file.
| Parameters | |
record:SessionRecord | Replacement record. |
| Returns | |
bool | Whether the session still existed and was updated. |
Serialise and persist record to disk atomically.
| Parameters | |
record:SessionRecord | The record to store. |
| Returns | |
None | Undocumented |