class Amber::Adapters::MemorySessionAdapter

Overview

In-memory implementation of SessionAdapter.

This adapter stores session data in memory using Hash structures and provides TTL functionality through background cleanup. This is the default session adapter and is suitable for development, testing, and single-instance applications.

It keeps at most #max_sessions sessions. When it is full, starting a new session drops the session used longest ago, so clients that never send their session cookie back cannot grow the process without limit. Reading, writing or extending a session counts as using it.

Note: Session data will be lost when the application restarts since everything is stored in memory. For production applications that require persistence or multi-instance deployments, consider using a database or Redis-based adapter.

Usage

adapter = Amber::Adapters::MemorySessionAdapter.new(max_sessions: 50_000)

Apps choose it with session.store: adapter and session.adapter: memory, and set the limit with session.max_memory_sessions.

Defined in:

amber/adapters/memory_session_adapter.cr

Constant Summary

DEFAULT_MAX_SESSIONS = 10000

The default for #max_sessions and for session.max_memory_sessions.

Constructors

Instance Method Summary

Instance methods inherited from class Amber::Adapters::SessionAdapter

batch(session_id : String, &block : SessionBatchOperations -> ) : Nil batch, batch_set(session_id : String, hash : Hash(String, String)) : Nil batch_set, close : Nil close, delete(session_id : String, key : String) : Nil delete, destroy(session_id : String) : Nil destroy, empty?(session_id : String) : Bool empty?, exists?(session_id : String, key : String) : Bool exists?, expire(session_id : String, seconds : Int32) : Nil expire, get(session_id : String, key : String) : String | Nil get, healthy? : Bool healthy?, keys(session_id : String) : Array(String) keys, set(session_id : String, key : String, value : String) : Nil set, to_hash(session_id : String) : Hash(String, String) to_hash, values(session_id : String) : Array(String) values

Constructor Detail

def self.new(max_sessions : Int32 = DEFAULT_MAX_SESSIONS) #

[View source]

Instance Method Detail

def batch(session_id : String, &block : BatchOperations -> ) : Nil #

Performs bulk operations atomically


[View source]
def batch_set(session_id : String, hash : Hash(String, String)) : Nil #

Efficiently sets multiple key-value pairs in a single atomic operation


[View source]
def delete(session_id : String, key : String) : Nil #

Deletes a specific key from a session


[View source]
def destroy(session_id : String) : Nil #

Completely destroys a session and all its data


[View source]
def empty?(session_id : String) : Bool #

Checks if the session is empty


[View source]
def exists?(session_id : String, key : String) : Bool #

Checks if a key exists in the given session


[View source]
def expire(session_id : String, seconds : Int32) : Nil #

Sets an expiration time for the session


[View source]
def get(session_id : String, key : String) : String | Nil #

Retrieves a value for the given session and key


[View source]
def keys(session_id : String) : Array(String) #

Returns all keys for the given session


[View source]
def max_sessions : Int32 #

The most sessions this adapter keeps at once.


[View source]
def set(session_id : String, key : String, value : String) : Nil #

Sets a value for the given session and key


[View source]
def size : Int32 #

Returns the number of sessions held, expired ones not yet removed included.


[View source]
def to_hash(session_id : String) : Hash(String, String) #

Returns all session data as a hash


[View source]
def values(session_id : String) : Array(String) #

Returns all values for the given session


[View source]