class ActiveRecord::SessionStore::SqlBypass
store in a blob
column instead and forgo the Base64 encoding.
binary session data in a text
column. For higher performance,
This serializing behavior is intended to store the widest range of
Marshal.load(::Base64.decode64(data))
and deserializing data is
::Base64.encode64(Marshal.dump(data))
serializing data is
are implemented as class methods that you may override. By default,
are configurable class attributes. Serializing and deserializeing
The database connection, table name, and session id and data columns
an example session model class meant as a basis for your own classes.
store but bypasses Active Record and issues SQL directly. This is
A barebones session store which duck-types with the default session
def connection
def connection @connection ||= ActiveRecord::Base.connection end
def connection_pool
def connection_pool @connection_pool ||= ActiveRecord::Base.connection_pool end
def data
def data unless @data if @serialized_data @data, @serialized_data = self.class.deserialize(@serialized_data) || {}, nil else @data = {} end end @data end
def destroy
def destroy return if @new_record connect = connection connect.delete <<-end_sql, 'Destroy session' DELETE FROM #{table_name} WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id.to_s)} end_sql end
def find_by_session_id(session_id)
def find_by_session_id(session_id) if record = connection.select_one("SELECT #{connection.quote_column_name(data_column)} AS data FROM #{@@table_name} WHERE #{connection.quote_column_name(@@session_id_column)}=#{connection.quote(session_id.to_s)}") new(:session_id => session_id, :serialized_data => record['data']) end end
def initialize(attributes)
telling us to postpone deserializing until the data is requested.
Look for normal and serialized data, self.find_by_session_id's way of
def initialize(attributes) @session_id = attributes[:session_id] @data = attributes[:data] @serialized_data = attributes[:serialized_data] @new_record = @serialized_data.nil? end
def loaded?
def loaded? @data end
def persisted?
def persisted? !@new_record end
def save
def save return false unless loaded? serialized_data = self.class.serialize(data) connect = connection if @new_record @new_record = false connect.update <<-end_sql, 'Create session' INSERT INTO #{table_name} ( #{connect.quote_column_name(session_id_column)}, #{connect.quote_column_name(data_column)} ) VALUES ( #{connect.quote(session_id)}, #{connect.quote(serialized_data)} ) end_sql else connect.update <<-end_sql, 'Update session' UPDATE #{table_name} SET #{connect.quote_column_name(data_column)}=#{connect.quote(serialized_data)} WHERE #{connect.quote_column_name(session_id_column)}=#{connect.quote(session_id)} end_sql end end