开发者

How to build your own custom session store class?

开发者 https://www.devze.com 2023-03-24 06:42 出处:网络
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 cust

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


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.


To implement your own session store, the easiest solution is to inherit from ActionDispatch::Session::AbstractStore and implement the necessary methods. The CookieStore is a quite simple implementation and should possibly get you started.

0

精彩评论

暂无评论...
验证码 换一张
取 消