2011-08-02 22 views
5

Theo mặc định, các phiên được lưu trữ trong cookie của trình duyệt (: cookie_store), nhưng bạn cũng có thể chỉ định một trong các cửa hàng được bao gồm khác:: active_record_store,: mem_cache_store hoặc lớp tùy chỉnh của riêng bạn Vui lòng cung cấp cho tôi cách xây dựng tùy chỉnh lớpCách tạo lớp lưu trữ phiên tùy chỉnh của riêng bạn?

config.action_controller.session_store = :your_customer_class 

Trả lời

5

Maurício Linhares là chính xác, tuy nhiên, tôi muốn thêm một số detai l bởi vì tôi không nghĩ rằng nó rõ ràng những phương pháp bạn cần phải thực hiện.

Bạn có thể kế thừa từ ActionDispatch::Session::AbstractStore, nhưng được kế thừa từ Rack::Session::Abstract::ID, đây là nơi tốt để tìm kiếm các phương pháp bạn cần triển khai. Cụ thể, từ 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 

Tôi đã viết đơn giản file-based session store làm thử nghiệm.

+0

Thừa kế 'Giá :: Phiên :: Tóm tắt :: ID' đã không được chấp nhận. Bây giờ nó sẽ là 'Rack :: Session :: Abstract :: Persisted'. –

+0

Ví dụ thực tế khác là đá quý ['redis-session-store'] (https://github.com/roidrage/redis-session-store). –

0

thực hiện lưu trữ phiên của riêng bạn, giải pháp đơn giản nhất là để kế thừa từ ActionDispatch::Session::AbstractStore và thực hiện các biện pháp cần thiết. các CookieStore là một thực hiện khá đơn giản và có thể sẽ giúp bạn bắt đầu.

Các vấn đề liên quan