Class: Redrack::Session::Middleware

Inherits:
Rack::Session::Abstract::ID
  • Object
show all
Defined in:
lib/redrack/session/middleware.rb

Overview

Redrack::Session::Middleware provides redis-based session-storage with the session ID stored in a cookie on the client’s browser.

This can be used much like the Rack::Session::Cookie or Rack::Session::Pool session storage middleware classes provided in the rack gem. This class supports the same options as these classes with the following additional options:

:redis_host -- hostname or IP of redis database server (default '127.0.0.1')
:redis_port -- port to connect to redis database server (default 6379)
:redis_path -- specify this if connecting to redis server via socket
:redis_database -- default redis database to hold sessions (default 0)
:redis_timeout -- default redis connection timeout in seconds (default 5)
:redis_namespace -- optional namespace under which session keys are stored
:redis_password -- optional authentication password for redis server

See the gem’s README.md for more information.

Constant Summary collapse

DEFAULT_OPTIONS =

redis-specific default options (in addition to those specified in Rack::Session::Abstract::ID::DEFAULT_OPTIONS)

Rack::Session::Abstract::ID::DEFAULT_OPTIONS.merge(
:redis_password  =>  nil,
:redis_namespace =>  nil,
:redis_path      =>  nil,
:redis_host      => "127.0.0.1",
:redis_port      =>  6379,
:redis_database  =>     0,
:redis_timeout   =>     5
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/redrack/session/middleware.rb', line 43

def initialize(app, options = {})
  super
  @mutex = Mutex.new

  # process redis-specific options
  if @default_options[:redis_path].is_a?(String)
    redis_options = { :path => @default_options[:redis_path] }
  else
    redis_options = {
      :host => (@default_options[:redis_host] || "127.0.0.1"),
      :port => (@default_options[:redis_port] ||  6379)
    }
  end
  redis_options[:db]       = @default_options[:redis_database] || 0
  redis_options[:timeout]  = @default_options[:redis_timeout]  || 5
  redis_options[:password] = @default_options[:redis_password] if @default_options[:redis_password].is_a?(String)

  # create connection to our redis database and ensure we're connected
  @redis = ::Redis.new(redis_options.merge(:thread_safe => true))
  @redis.ping

  # store session keys under specified namespace (if any)
  if @default_options[:redis_namespace]
    @redis = ::Redis::Namespace.new(@default_options[:redis_namespace], :redis => @redis)
  end
end

Instance Attribute Details

#redisObject (readonly)

provide raw access to redis database (for testing)



29
30
31
# File 'lib/redrack/session/middleware.rb', line 29

def redis
  @redis
end