2011-08-02 15 views
5

डिफ़ॉल्ट रूप से, ब्राउज़र कुकीज़ (: cookie_store) में सत्र संग्रहीत होते हैं, लेकिन आप अन्य शामिल स्टोरों में से एक को भी निर्दिष्ट कर सकते हैं (: active_record_store,: mem_cache_store, या अपनी खुद की कस्टम क्लास। कृपया मुझे कस्टम बनाने का तरीका दें वर्गअपनी खुद की कस्टम सत्र स्टोर कक्षा कैसे बनाएं?

config.action_controller.session_store = :your_customer_class 

उत्तर

5

Maurício लिन्हारेस है सही, हालांकि, मैं कुछ 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

'रैक :: सत्र :: सार :: आईडी' को हटा दिया गया है। अब यह 'रैक :: सत्र :: सार :: स्थिर' होना चाहिए। –

+0

एक और वास्तविक जीवन उदाहरण ['redis-session-store'] है (https://github.com/roidrage/redis-session-store) मणि। –

0

अपने स्वयं के सत्र की दुकान को लागू करने के लिए, सबसे आसान समाधान ActionDispatch::Session::AbstractStore से विरासत और आवश्यक तरीकों को लागू करने के लिए है। CookieStore एक काफी सरल कार्यान्वयन है और संभवतः आप कार्य आरंभ कर देना चाहिए।

संबंधित मुद्दे