Class: RedisSessionStore

Inherits:
ActionDispatch::Session::AbstractSecureStore
  • 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.11.6'.freeze
ENV_SESSION_OPTIONS_KEY =
if Rack.release.split('.').first.to_i > 1
  Rack::RACK_SESSION_OPTIONS
else
  Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
end
USE_INDIFFERENT_ACCESS =
defined?(ActiveSupport).freeze

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

Rails.application.config.session_store :redis_session_store,
  key: 'your_session_key',
  redis: {
    expire_after: 120.minutes,
    key_prefix: 'myapp:session:',
    url: 'redis://localhost:6379/0'
  },
  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
# File 'lib/redis-session-store.rb', line 40

def initialize(app, options = {})
  super

  @default_options[:namespace] = 'rack:session'
  @default_options.merge!(options[:redis] || {})
  init_options = options[:redis]&.reject { |k, _v| %i[expire_after key_prefix].include?(k) } || {}
  @redis = init_options[:client] || Redis.new(init_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.



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

def on_redis_down
  @on_redis_down
end

#on_session_load_errorObject

Returns the value of attribute on_session_load_error.



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

def on_session_load_error
  @on_session_load_error
end