Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build your own custom session store class?

By default, sessions are stored in browser cookies (:cookie_store), but you can also specify one of the other included stores (:active_record_store, :mem_cache_store, or your own custom class. Please give me the way to build custom class

config.action_controller.session_store = :your_customer_class
like image 956
khanh Avatar asked Aug 02 '11 08:08

khanh


1 Answers

Maurício Linhares is correct, however, I wanted to add some detail because I don't think it's obvious which methods you need to implement.

You can inherit from ActionDispatch::Session::AbstractStore, but that inherits from Rack::Session::Abstract::ID which is a good place to look for the methods you need to implement. Specifically, from Rack::Session::Abstract::ID:

# All thread safety and session retrival proceedures should occur here.
# Should return [session_id, session].
# If nil is provided as the session id, generation of a new valid id
# should occur within.

def get_session(env, sid)
  raise '#get_session not implemented.'
end

# All thread safety and session storage proceedures should occur here.
# Should return true or false dependant on whether or not the session
# was saved or not.

def set_session(env, sid, session, options)
  raise '#set_session not implemented.'
end

# All thread safety and session destroy proceedures should occur here.
# Should return a new session id or nil if options[:drop]

def destroy_session(env, sid, options)
  raise '#destroy_session not implemented'
end

I wrote a simple file-based session store as an experiment.

like image 63
Brad Pauly Avatar answered Nov 01 '22 08:11

Brad Pauly