Class: Rack::Session::Moneta

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/rack/session/moneta.rb

Direct Known Subclasses

ActionDispatch::Session::MonetaStore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ Moneta

Returns a new instance of Moneta


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/session/moneta.rb', line 11

def initialize(app, options = {}, &block)
  super
  if block
    raise ArgumentError, 'Use either block or option :store' if options[:store]
    @pool = ::Moneta.build(&block)
  else
    raise ArgumentError, 'Block or option :store is required' unless @pool = options[:store]
    @pool = ::Moneta.new(@pool, :expires => true) if Symbol === @pool
  end
  @mutex = Mutex.new
end

Instance Attribute Details

#mutexObject (readonly)


9
10
11
# File 'lib/rack/session/moneta.rb', line 9

def mutex
  @mutex
end

#poolObject (readonly)


9
10
11
# File 'lib/rack/session/moneta.rb', line 9

def pool
  @pool
end

Instance Method Details

#destroy_session(env, session_id, options) ⇒ Object


48
49
50
51
52
53
# File 'lib/rack/session/moneta.rb', line 48

def destroy_session(env, session_id, options)
  with_lock(env) do
    @pool.delete(session_id)
    generate_sid unless options[:drop]
  end
end

#generate_sidObject


23
24
25
26
27
28
# File 'lib/rack/session/moneta.rb', line 23

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

#get_session(env, sid) ⇒ Object


30
31
32
33
34
35
36
37
38
# File 'lib/rack/session/moneta.rb', line 30

def get_session(env, sid)
  with_lock(env) do
    unless sid && session = @pool[sid]
      sid, session = generate_sid, {}
      @pool[sid] = session
    end
    [sid, session]
  end
end

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


40
41
42
43
44
45
46
# File 'lib/rack/session/moneta.rb', line 40

def set_session(env, session_id, new_session, options)
  with_lock(env) do
    @pool.store(session_id, new_session,
                 options[:expire_after] ? {:expires => options[:expire_after]} : {})
    session_id
  end
end

#with_lock(env) ⇒ Object


55
56
57
58
59
60
# File 'lib/rack/session/moneta.rb', line 55

def with_lock(env)
  @mutex.lock if env['rack.multithread']
  yield
ensure
  @mutex.unlock if @mutex.locked?
end