Class: RedisSessionStore
- Inherits:
-
ActionDispatch::Session::AbstractStore
- Object
- ActionDispatch::Session::AbstractStore
- RedisSessionStore
- 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
- 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
-
#on_redis_down ⇒ Object
Returns the value of attribute on_redis_down.
-
#on_session_load_error ⇒ Object
Returns the value of attribute on_session_load_error.
Instance Method Summary collapse
-
#initialize(app, options = {}) ⇒ RedisSessionStore
constructor
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.
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 |
# File 'lib/redis-session-store.rb', line 40 def initialize(app, = {}) super @default_options.merge!([:redis] || {}) = [:redis]&.reject { |k, _v| %i[expire_after key_prefix].include?(k) } || {} @redis = [:client] || Redis.new() @on_redis_down = [:on_redis_down] @serializer = determine_serializer([:serializer]) @on_session_load_error = [:on_session_load_error] verify_handlers! end |
Instance Attribute Details
#on_redis_down ⇒ Object
Returns the value of attribute on_redis_down.
52 53 54 |
# File 'lib/redis-session-store.rb', line 52 def on_redis_down @on_redis_down end |
#on_session_load_error ⇒ Object
Returns the value of attribute on_session_load_error.
52 53 54 |
# File 'lib/redis-session-store.rb', line 52 def on_session_load_error @on_session_load_error end |