abstract class Amber::Adapters::SessionAdapter

Overview

Abstract base class for session storage adapters.

All session storage implementations should inherit from this class and implement the required abstract methods. This allows the Amber framework to work with any backend storage system (Redis, database, memory, etc.) through a unified interface.

Example implementation:

class CustomSessionAdapter < Amber::Adapters::SessionAdapter
  def initialize(@connection : MyStorageConnection)
  end

  def get(session_id : String, key : String) : String?
    @connection.fetch("#{session_id}:#{key}")
  end

  # ... implement other abstract methods
end

Direct Known Subclasses

Defined in:

amber/adapters/session_adapter.cr

Instance Method Summary

Instance Method Detail

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

Provides a way to perform multiple operations in a single atomic block. This is useful for adapters that support pipelining or transactions.


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

Efficiently sets multiple key-value pairs in a single operation. This should be atomic where possible to maintain session consistency.


[View source]
def close : Nil #

Called when the adapter is being shut down. Override this method to perform cleanup operations like closing connections.


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

Deletes a specific key from the session. Should be a no-op if the key doesn't exist.


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

Completely destroys the session and all its data.


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

Checks if the session is empty (contains no keys). Note: Some implementations may consider a session with only metadata (like session_id) as empty for application purposes.


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

Checks if a specific key exists within the session.


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

Sets an expiration time for the session in seconds from now. A value of 0 or negative should remove any existing expiration.


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

Retrieves a value for the given key within the specified session. Returns the value as a String, or nil if the key doesn't exist.


[View source]
def healthy? : Bool #

Called to check if the adapter is healthy and ready to handle requests. Override this method to implement health checks for your storage backend.


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

Returns all keys present in the session.


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

Sets a value for the given key within the specified session.


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

Returns the entire session as a Hash.


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

Returns all values present in the session.


[View source]