Class: Rack::Session::Moneta

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

Overview

Rack::Session::Moneta stores sessions in a Moneta store.

Example:

use Rack::Session::Moneta, key: 'rack.session', domain: 'foo.com', path: '/', expire_after: 2592000, store: Moneta.new(…)

You can use all options supported by `Rack::Session::Abstract::ID`.

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


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/session/moneta.rb', line 23

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
  @pool = ::Moneta::WeakCreate.new(@pool) unless @pool.supports?(:create)
  @mutex = ::Mutex.new
end

Instance Attribute Details

#mutexObject (readonly)


21
22
23
# File 'lib/rack/session/moneta.rb', line 21

def mutex
  @mutex
end

#poolObject (readonly)


21
22
23
# File 'lib/rack/session/moneta.rb', line 21

def pool
  @pool
end

Instance Method Details

#destroy_session(env, session_id, options) ⇒ Object


63
64
65
66
67
68
# File 'lib/rack/session/moneta.rb', line 63

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

#generate_sidObject


36
37
38
39
40
41
# File 'lib/rack/session/moneta.rb', line 36

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

#get_session(env, sid) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/session/moneta.rb', line 43

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

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


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

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


70
71
72
73
74
75
# File 'lib/rack/session/moneta.rb', line 70

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