2011-08-02 11 views
5

デフォルトでは、セッションは、ブラウザのクッキーに保存されていますが、他の含ま店(のいずれかを指定することができます:active_record_store、:。mem_cache_store、または独自のカスタムクラスをは私にカスタムを構築する方法を教えてください最も簡単な解決策はActionDispatch::Session::AbstractStoreから継承し、必要なメソッドを実装することで、独自のセッションストアを実装するクラス独自のカスタムセッションストアクラスを構築するにはどうすればよいですか? (:cookie_store)

config.action_controller.session_store = :your_customer_class 

答えて

5

マウリシオ・リニャレスされます正しい、しかし、私はいくつかのdetaiを追加したいなぜなら、どのメソッドを実装する必要があるのか​​は明らかではないと思うからです。

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)宝石です。 –

0

CookieStoreは非常に単純な実装で、おそらくあなたが始める必要があります。

関連する問題