Class: Rack::Session::Riak

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/riak_sessions.rb

Constant Summary collapse

DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge(
  :drop         => false,
  :riak_server  => '127.0.0.1',
  :riak_port    => 8098
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Riak.



59
60
61
62
63
64
65
# File 'lib/riak_sessions.rb', line 59

def initialize(app, options={})
  super

  @pool = RiakPool.new(@default_options[:riak_server],
                       @default_options[:riak_port])
  @mutex = Mutex.new
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex.



52
53
54
# File 'lib/riak_sessions.rb', line 52

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



52
53
54
# File 'lib/riak_sessions.rb', line 52

def pool
  @pool
end

Instance Method Details

#get_session(env, sid) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/riak_sessions.rb', line 67

def get_session(env, sid)
  session = @pool[sid] if sid
  @mutex.lock if env['rack.multithread']

  unless sid and session
    env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil?
    session = {}
    sid = generate_sid
    @pool.store sid, session
  end

  return [sid, session]
ensure
  @mutex.unlock if env['rack.multithread']
end

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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/riak_sessions.rb', line 83

def set_session(env, session_id, new_session, options)
  @mutex.lock if env['rack.multithread']

  if options[:renew] or options[:drop]
    @pool.delete session_id
    return false if options[:drop]
    session_id = generate_sid
    @pool.store session_id, 0
  end

  @pool.store session_id, new_session
  return session_id
rescue
  warn "#{new_session.inspect} has been lost."
  warn $!.inspect
ensure
  @mutex.unlock if env['rack.multithread']
end