2011-08-02 67 views
5

默認情況下,會話存儲在瀏覽器cookie(:cookie_store)中,但您也可以指定其他包含的商店之一(:active_record_store,:mem_cache_store,或您自己的自定義類。請給我建立自定義類如何構建自己的自定義會話存儲類?

config.action_controller.session_store = :your_customer_class 

回答

5

毛裏西奧利尼亞雷斯是正確的,但是,我想添加一些詳細信息因爲我不認爲你需要實現哪些方法是顯而易見的。

您可以從ActionDispatch::Session::AbstractStore繼承,但繼承自Rack::Session::Abstract::ID,這是一個尋找您需要實現的方法的好地方。具體地,從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 

我寫了一個簡單file-based session store作爲實驗。

+0

繼承'Rack :: Session :: Abstract :: ID'已被棄用。現在它應該是'Rack :: Session :: Abstract :: Persisted'。 –

+0

另一個真實的例子是['redis-session-store'](https://github.com/roidrage/redis-session-store)gem。 –

相關問題