class ActiveRecord::SessionStore
use it as a basis for high-performance database-specific stores.
The example SqlBypass class is a generic SQL session store. You may
destroy
save
attr_accessor :data
attr_reader :session_id
initialize(hash_of_session_id_and_data)
self.find_by_session_id(session_id)
You must implement these methods:
ActiveRecord::SessionStore.session_class = MySessionClass
store, by setting
feature-packed Active Record or a bare-metal high-performance SQL
You may provide your own session class implementation, whether a
the sessions
table, making periodic session expiration a snap.
for free if you add created_at
and updated_at
datetime columns to
Since the default class is a simple Active Record, you get timestamps
on ApplicationController is a good place.
set session.model.id = session.session_id
by hand! A before filter
having a separate id
column if you don’t want it. However, you must
Note that setting the primary key to the session_id
frees you from
ActiveRecord::SessionStore::Session.data_column_name = ‘legacy_session_data’
ActiveRecord::SessionStore::Session.primary_key = ‘session_id’
ActiveRecord::SessionStore::Session.table_name = ‘legacy_session_table’
For example, at the end of config/environment.rb
:
You may configure the table name, primary key, and data column.
ActionController::SessionOverflowError will be raised.
If the data you write is larger than the column’s size limit,
Session data is marshaled to the data
column in Base64 format.
The session_id
column should always be indexed for speedy lookups.data
(text or longtext; careful if your session data exceeds 65KB).session_id
(text, or longtext if your session data exceeds 65K), andid
(numeric primary key),
The default assumes a sessions
tables with columns:
with text session_id
and data
attributes is sufficient.
provided, but any object duck-typing to an Active Record Session class
A session store backed by an Active Record class. A default class is
def destroy(env)
def destroy(env) if sid = current_session_id(env) Base.silence do get_session_model(env, sid).destroy end end end
def find_session(id)
def find_session(id) @@session_class.find_by_session_id(id) || @@session_class.new(:session_id => id, :data => {}) end
def get_session(env, sid)
def get_session(env, sid) Base.silence do sid ||= generate_sid session = find_session(sid) env[SESSION_RECORD_KEY] = session [sid, session.data] end end
def get_session_model(env, sid)
def get_session_model(env, sid) if env[ENV_SESSION_OPTIONS_KEY][:id].nil? env[SESSION_RECORD_KEY] = find_session(sid) else env[SESSION_RECORD_KEY] ||= find_session(sid) end end
def set_session(env, sid, session_data)
def set_session(env, sid, session_data) Base.silence do record = get_session_model(env, sid) record.data = session_data return false unless record.save session_data = record.data if session_data && session_data.respond_to?(:each_value) session_data.each_value do |obj| obj.clear_association_cache if obj.respond_to?(:clear_association_cache) end end end return true end