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.
Note that memcache does drop data before it may be listed to expire. For a full description of behaviour, please see memcache’s documentation.
(Not documented)
# File lib/rack/session/memcache.rb, line 28 28: def initialize(app, options={}) 29: super 30: 31: @mutex = Mutex.new 32: @pool = MemCache. 33: new @default_options[:memcache_server], @default_options 34: raise 'No memcache servers' unless @pool.servers.any?{|s|s.alive?} 35: end
(Not documented)
# File lib/rack/session/memcache.rb, line 37 37: def generate_sid 38: loop do 39: sid = super 40: break sid unless @pool.get(sid, true) 41: end 42: end
(Not documented)
# File lib/rack/session/memcache.rb, line 44 44: def get_session(env, sid) 45: session = @pool.get(sid) if sid 46: @mutex.lock if env['rack.multithread'] 47: unless sid and session 48: env['rack.errors'].puts("Session '#{sid.inspect}' not found, initializing...") if $VERBOSE and not sid.nil? 49: session = {} 50: sid = generate_sid 51: ret = @pool.add sid, session 52: raise "Session collision on '#{sid.inspect}'" unless /^STORED/ =~ ret 53: end 54: session.instance_variable_set('@old', {}.merge(session)) 55: return [sid, session] 56: rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted 57: warn "#{self} is unable to find server." 58: warn $!.inspect 59: return [ nil, {} ] 60: ensure 61: @mutex.unlock if env['rack.multithread'] 62: end
(Not documented)
# File lib/rack/session/memcache.rb, line 64 64: def set_session(env, session_id, new_session, options) 65: expiry = options[:expire_after] 66: expiry = expiry.nil? ? 0 : expiry + 1 67: 68: @mutex.lock if env['rack.multithread'] 69: session = @pool.get(session_id) || {} 70: if options[:renew] or options[:drop] 71: @pool.delete session_id 72: return false if options[:drop] 73: session_id = generate_sid 74: @pool.add session_id, 0 # so we don't worry about cache miss on #set 75: end 76: old_session = new_session.instance_variable_get('@old') || {} 77: session = merge_sessions session_id, old_session, new_session, session 78: @pool.set session_id, session, expiry 79: return session_id 80: rescue MemCache::MemCacheError, Errno::ECONNREFUSED # MemCache server cannot be contacted 81: warn "#{self} is unable to find server." 82: warn $!.inspect 83: return false 84: ensure 85: @mutex.unlock if env['rack.multithread'] 86: end
(Not documented)
# File lib/rack/session/memcache.rb, line 90 90: def merge_sessions sid, old, new, cur=nil 91: cur ||= {} 92: unless Hash === old and Hash === new 93: warn 'Bad old or new sessions provided.' 94: return cur 95: end 96: 97: delete = old.keys - new.keys 98: warn "//@#{sid}: delete #{delete*','}" if $VERBOSE and not delete.empty? 99: delete.each{|k| cur.delete k } 100: 101: update = new.keys.select{|k| new[k] != old[k] } 102: warn "//@#{sid}: update #{update*','}" if $VERBOSE and not update.empty? 103: update.each{|k| cur[k] = new[k] } 104: 105: cur 106: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.