Class: RedisSessionStore

Inherits:
ActionDispatch::Session::AbstractStore
  • Object
show all
Defined in:
lib/redis-session-store.rb

Overview

Redis session storage for Rails, and for Rails only. Derived from the MemCacheStore code, simply dropping in Redis instead.

Defined Under Namespace

Classes: HybridSerializer, JsonSerializer

Constant Summary collapse

VERSION =
'0.9.1'.freeze
ENV_SESSION_OPTIONS_KEY =
Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RedisSessionStore

Options

  • :key - Same as with the other cookie stores, key name

  • :redis - A hash with redis-specific options

    • :url - Redis url, default is redis://localhost:6379/0

    • :key_prefix - Prefix for keys used in Redis, e.g. myapp:

    • :expire_after - A number in seconds for session timeout

    • :client - Connect to Redis with given object rather than create one

  • :on_redis_down: - Called with err, env, and SID on Errno::ECONNREFUSED

  • :on_session_load_error: - Called with err and SID on Marshal.load fail

  • :serializer: - Serializer to use on session data, default is :marshal.

Examples

My::Application.config.session_store :redis_session_store, {
  key: 'your_session_key',
  redis: {
    expire_after: 120.minutes,
    key_prefix: 'myapp:session:',
    url: 'redis://host:12345/2'
  },
  on_redis_down: ->(*a) { logger.error("Redis down! #{a.inspect}") }
  serializer: :hybrid # migrate from Marshal to JSON
}


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/redis-session-store.rb', line 40

def initialize(app, options = {})
  super

  redis_options = options[:redis] || {}

  @default_options[:namespace] = 'rack:session'
  @default_options.merge!(redis_options)
  @redis = redis_options[:client] || Redis.new(redis_options)
  @on_redis_down = options[:on_redis_down]
  @serializer = determine_serializer(options[:serializer])
  @on_session_load_error = options[:on_session_load_error]
  verify_handlers!
end

Instance Attribute Details

#on_redis_downObject

Returns the value of attribute on_redis_down.



54
55
56
# File 'lib/redis-session-store.rb', line 54

def on_redis_down
  @on_redis_down
end

#on_session_load_errorObject

Returns the value of attribute on_session_load_error.



54
55
56
# File 'lib/redis-session-store.rb', line 54

def on_session_load_error
  @on_session_load_error
end