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.crConstant Summary
-
DEFAULT_MAX_SESSIONS =
10000 -
The default for
#max_sessionsand forsession.max_memory_sessions.
Constructors
Instance Method Summary
-
#batch(session_id : String, &block : BatchOperations -> ) : Nil
Performs bulk operations atomically
-
#batch_set(session_id : String, hash : Hash(String, String)) : Nil
Efficiently sets multiple key-value pairs in a single atomic operation
-
#delete(session_id : String, key : String) : Nil
Deletes a specific key from a session
-
#destroy(session_id : String) : Nil
Completely destroys a session and all its data
-
#empty?(session_id : String) : Bool
Checks if the session is empty
-
#exists?(session_id : String, key : String) : Bool
Checks if a key exists in the given session
-
#expire(session_id : String, seconds : Int32) : Nil
Sets an expiration time for the session
-
#get(session_id : String, key : String) : String | Nil
Retrieves a value for the given session and key
-
#keys(session_id : String) : Array(String)
Returns all keys for the given session
-
#max_sessions : Int32
The most sessions this adapter keeps at once.
-
#set(session_id : String, key : String, value : String) : Nil
Sets a value for the given session and key
-
#size : Int32
Returns the number of sessions held, expired ones not yet removed included.
-
#to_hash(session_id : String) : Hash(String, String)
Returns all session data as a hash
-
#values(session_id : String) : Array(String)
Returns all values for the given session
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
Instance Method Detail
Performs bulk operations atomically
Efficiently sets multiple key-value pairs in a single atomic operation
Deletes a specific key from a session
Completely destroys a session and all its data
Checks if a key exists in the given session
Sets an expiration time for the session
Retrieves a value for the given session and key
Returns all keys for the given session
Sets a value for the given session and key
Returns all session data as a hash
Returns all values for the given session