class Strelka::Session::Db
Database session class – this class offers persistent session data using any database that Sequel supports. It defaults to non-persistent, in memory Sqlite.
Constants
- CONFIG_DEFAULTS
Configuration defaults
Public Class Methods
Configure the session class with the given options
, which should be a Hash or an object that has a Hash-like interface.
Valid options (in addition to those ):
- [cookie_name]
-
The name of the cookie to use for the session ID
- [cookie_options]
-
Options to pass to Strelka::Cookie's constructor.
- [connect]
-
The Sequel connection string; if nil, an in-memory DB will be used.
- [table_name]
-
The name of the sessions table. Defaults to 'sessions'.
Strelka::Session::Default::configure
# File lib/strelka/session/db.rb, line 137 def self::configure( options=nil ) super if options && options[:connect] self.log.warn "Configuring dbsession with: %p" % [ options ] self.table_name = options[:table_name] || CONFIG_DEFAULTS[:table_name] self.db = Sequel.connect( options[:connect] ) self.db.logger = Loggability[ Mongrel2 ].proxy_for( self.db ) self.db.sql_log_level = :debug self.initialize_sessions_table end end
Delete the data associated with the given session_id
from the DB.
# File lib/strelka/session/db.rb, line 111 def self::delete_session_data( session_id ) self.dataset.filter( :session_id => session_id ).delete end
Return true
if the given request
has a session token which corresponds to an existing session key.
# File lib/strelka/session/db.rb, line 118 def self::has_session_for?( request ) id = self.get_existing_session_id( request ) or return false return !self.dataset.filter( :session_id => id ).empty? end
Create the initial DB sessions schema if needed, setting the sessions
attribute to a Sequel dataset on the configured DB table.
# File lib/strelka/session/db.rb, line 68 def self::initialize_sessions_table if self.db.table_exists?( self.table_name.to_sym ) self.log.debug "Using existing sessions table for %p" % [ db ] else self.log.debug "Creating new sessions table for %p" % [ db ] self.db.create_table( self.table_name.to_sym ) do text :session_id, :primary_key => true text :session timestamp :created end end self.dataset = self.db[ self.table_name.to_sym ] end
Load a session instance from storage using the given session_id
.
# File lib/strelka/session/db.rb, line 86 def self::load( session_id ) session_row = self.dataset.filter( :session_id => session_id ).first session = if session_row.nil? {} else YAML.load( session_row[:session], safe: false ) end return new( session_id, session ) end
Save the given data
associated with the session_id
to the DB.
# File lib/strelka/session/db.rb, line 98 def self::save_session_data( session_id, data={} ) self.db.transaction do self.delete_session_data( session_id.to_s ) self.dataset.insert( :session_id => session_id, :session => data.to_yaml, :created => Time.now.utc.to_s ) end end
Public Instance Methods
The Sequel dataset for the sessions table
# File lib/strelka/session/db.rb, line 50 singleton_attr_accessor :dataset
The Sequel dataset connection
# File lib/strelka/session/db.rb, line 46 singleton_attr_accessor :db
Configurability API – use the 'dbsession' section of the config
# File lib/strelka/session/db.rb, line 25 config_key :dbsession
The name of the table to use for storing sessions
# File lib/strelka/session/db.rb, line 54 singleton_attr_accessor :table_name