Abstract::ID
Rack::Session::Memcache provides simple cookie based session management. Session data is stored in memcached. The corresponding session key is maintained in the cookie. You may treat Session::Memcache as you would Session::Pool with the following caveats.
Setting :expire_after to 0 would note to the Memcache server to hang onto the session data until it would drop it according to it’s own specifications. However, the cookie sent to the client would expire immediately.
Note that memcache does drop data before it may be listed to expire. For a full description of behaviour, please see memcache’s documentation.
# File lib/rack/session/memcache.rb, line 29 29: def initialize(app, options={}) 30: super 31: 32: @mutex = Mutex.new 33: mserv = @default_options[:memcache_server] 34: mopts = @default_options.reject{|k,v| !MemCache::DEFAULT_OPTIONS.include? k } 35: 36: @pool = options[:cache] || MemCache.new(mserv, mopts) 37: unless @pool.active? and @pool.servers.any?{|c| c.alive? } 38: raise 'No memcache servers' 39: end 40: end
# File lib/rack/session/memcache.rb, line 71 71: def destroy_session(env, session_id, options) 72: with_lock(env) do 73: @pool.delete(session_id) 74: generate_sid unless options[:drop] 75: end 76: end
# File lib/rack/session/memcache.rb, line 42 42: def generate_sid 43: loop do 44: sid = super 45: break sid unless @pool.get(sid, true) 46: end 47: end
# File lib/rack/session/memcache.rb, line 49 49: def get_session(env, sid) 50: with_lock(env, [nil, {}]) do 51: unless sid and session = @pool.get(sid) 52: sid, session = generate_sid, {} 53: unless /^STORED/ =~ @pool.add(sid, session) 54: raise "Session collision on '#{sid.inspect}'" 55: end 56: end 57: [sid, session] 58: end 59: end
# File lib/rack/session/memcache.rb, line 61 61: def set_session(env, session_id, new_session, options) 62: expiry = options[:expire_after] 63: expiry = expiry.nil? ? 0 : expiry + 1 64: 65: with_lock(env, false) do 66: @pool.set session_id, new_session, expiry 67: session_id 68: end 69: end
# File lib/rack/session/memcache.rb, line 78 78: def with_lock(env, default=nil) 79: @mutex.lock if env['rack.multithread'] 80: yield 81: rescue MemCache::MemCacheError, Errno::ECONNREFUSED 82: if $VERBOSE 83: warn "#{self} is unable to find memcached server." 84: warn $!.inspect 85: end 86: default 87: ensure 88: @mutex.unlock if @mutex.locked? 89: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.