lib/rack/session/rails.rb in redis-store-0.3.9 vs lib/rack/session/rails.rb in redis-store-1.0.0.beta1

- old
+ new

@@ -1,61 +1,70 @@ -module ActionController - module Session -# Redis session storage for Rails, and for Rails only. Derived from -# the MemCacheStore code, simply dropping in Redis instead. -# -# Options: -# :key => Same as with the other cookie stores, key name -# :secret => Encryption secret for the key -# :host => Redis host name, default is localhost -# :port => Redis port, default is 6379 -# :db => Database number, defaults to 0. Useful to separate your session storage from other data -# :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others -# :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis +module RedisStore + module Rack + module Session + # Redis session storage for Rails, and for Rails only. Derived from + # the MemCacheStore code, simply dropping in Redis instead. + # + # Options: + # :key => Same as with the other cookie stores, key name + # :secret => Encryption secret for the key + # :host => Redis host name, default is localhost + # :port => Redis port, default is 6379 + # :db => Database number, defaults to 0. Useful to separate your session storage from other data + # :key_prefix => Prefix for keys used in Redis, e.g. myapp-. Useful to separate session storage keys visibly from others + # :expire_after => A number in seconds to set the timeout interval for the session. Will map directly to expiry in Redis + module Rails + def initialize(app, options = {}) + # Support old :expires option + options[:expire_after] ||= options[:expires] - class RedisSessionStore < ActionController::Session::AbstractStore + super - def initialize(app, options = {}) - # Support old :expires option - options[:expire_after] ||= options[:expires] + @options = { :key_prefix => "" }.update(options) + servers = [options[:servers]].flatten.compact.map do |server_options| + { + :namespace => 'rack:session', + :host => 'localhost', + :port => '6379', + :db => 0 + }.update(Redis::Factory.convert_to_redis_client_options(server_options)) + end - super + @pool = Redis::Factory.create(*servers) + end - @options = { :key_prefix => "" }.update(options) - servers = [options[:servers]].flatten.compact.map do |server_options| - { - :namespace => 'rack:session', - :host => 'localhost', - :port => '6379', - :db => 0 - }.update(Redis::Factory.convert_to_redis_client_options(server_options)) + private + def prefixed(sid) + "#{@options[:key_prefix]}#{sid}" end - @pool = Redis::Factory.create(*servers) - end + def get_session(env, sid) + sid ||= generate_sid + begin + session = @pool.marshalled_get(prefixed(sid)) || {} + rescue Errno::ECONNREFUSED + session = {} + end + [sid, session] + end - private - def prefixed(sid) - "#{@options[:key_prefix]}#{sid}" - end - - def get_session(env, sid) - sid ||= generate_sid - begin - session = @pool.marshalled_get(prefixed(sid)) || {} + def set_session(env, sid, session_data) + options = env['rack.session.options'] + @pool.marshalled_set(prefixed(sid), session_data, options) + return true rescue Errno::ECONNREFUSED - session = {} + return false end - [sid, session] end - - def set_session(env, sid, session_data) - options = env['rack.session.options'] - @pool.marshalled_set(prefixed(sid), session_data, options) - return true - rescue Errno::ECONNREFUSED - return false - end - end + end +end + +unless defined?(ActionDispatch) # rails 2.3.x + class ActionController::Session::RedisSessionStore < ActionController::Session::AbstractStore + include RedisStore::Rack::Session::Rails + end +else # rails 3.x + class ActionDispatch::Session::RedisSessionStore < ActionDispatch::Session::AbstractStore + include RedisStore::Rack::Session::Rails end end