Class: UzuUzu::RackSession::Memcache

Inherits:
Rack::Session::Abstract::ID
  • Object
show all
Defined in:
lib/uzuuzu-core/rack_session/memcache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Memcache

Returns a new instance of Memcache.



13
14
15
16
17
18
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 13

def initialize(env=nil)
  @adapter = :memcache
  super(nil, env)
  @mutex = Mutex.new
  @pool = ::UzuUzu::Wrapper::Wrapper.new('memcache', env['config']).wrapper
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 8

def adapter
  @adapter
end

Instance Method Details

#app(app) ⇒ Object



20
21
22
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 20

def app(app)
  @app = app
end

#destroy_session(env, session_id, options) ⇒ Object



50
51
52
53
54
55
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 50

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

#generate_sidObject



24
25
26
27
28
29
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 24

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

#get_session(env, sid) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 31

def get_session(env, sid)
  with_lock(env, [nil, {}]) do
    unless sid and session = @pool.get(sid)
      sid, session = generate_sid, {}
      unless /^STORED/ =~ @pool.add(sid, session)
        raise "Session collision on '#{sid.inspect}'"
      end
    end
    [sid, session]
  end
end

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



43
44
45
46
47
48
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 43

def set_session(env, session_id, new_session, options)
  with_lock(env, false) do
    @pool.set session_id, new_session
    session_id
  end
end

#with_lock(env, default = nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/uzuuzu-core/rack_session/memcache.rb', line 57

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