Class: Rack::Session::Redis

Inherits:
Abstract::ID
  • Object
show all
Includes:
Merb::RedisStore
Defined in:
lib/rack/session/redis.rb,
lib/rack/session/merb.rb

Overview

Redis session storage for Rack applications.

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

Constant Summary collapse

DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge :redis_server => "localhost:6379"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Merb::RedisStore

#delete_session, #retrieve_session, #store_session

Constructor Details

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

Returns a new instance of Redis.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rack/session/redis.rb', line 17

def initialize(app, options = {})
  super
  @mutex = Mutex.new
  @key_prefix = options[:key_prefix] || ""
  servers = [options[:servers]].flatten.compact.map do |server_options|
    {
      :namespace => 'rack:session',
      :host => 'localhost',
      :port => '6379',
      :db => 0
    }.update(RedisFactory.convert_to_redis_client_options(server_options))
  end
  @pool = RedisFactory.create(*servers) || @default_options[:redis_server]
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex.



14
15
16
# File 'lib/rack/session/redis.rb', line 14

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



14
15
16
# File 'lib/rack/session/redis.rb', line 14

def pool
  @pool
end

Instance Method Details

#generate_sidObject



32
33
34
35
36
37
# File 'lib/rack/session/redis.rb', line 32

def generate_sid
  loop do
    sid = super
    break sid unless @pool.marshalled_get(sid)
  end
end

#get_session(env, sid) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/session/redis.rb', line 39

def get_session(env, sid)
  session = @pool.marshalled_get(prefixed(sid)) if sid
  @mutex.lock if env['rack.multithread']
  unless sid and session
    env['rack.errors'].puts("Session '#{prefixed(sid).inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
    session = {}
    sid = generate_sid
    ret = @pool.marshalled_set prefixed(sid), session
    raise "Session collision on '#{prefixed(sid).inspect}'" unless ret
  end
  return [sid, session]
rescue Errno::ECONNREFUSED
  warn "#{self} is unable to find server."
  warn $!.inspect
  return [ nil, {} ]
ensure
  @mutex.unlock if env['rack.multithread']
end

#set_session(env, session_id, new_session, options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rack/session/redis.rb', line 58

def set_session(env, session_id, new_session, options)
  @mutex.lock if env['rack.multithread']
  session = @pool.marshalled_get(session_id) rescue {}
  if options[:renew] or options[:drop]
    @pool.del session_id
    return false if options[:drop]
    session_id = generate_sid
    @pool.marshalled_set prefixed(session_id), 0
  end
  @pool.marshalled_set prefixed(session_id), new_session, options
  return session_id
rescue Errno::ECONNREFUSED
  warn "#{self} is unable to find server."
  warn $!.inspect
  return false
ensure
  @mutex.unlock if env['rack.multithread']
end