4a65cbf35d31cded769bde51e27c4ffd9096078d
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / session / memcache.rb
1 # AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
2
3 require 'rack/session/abstract/id'
4 require 'memcache'
5
6 module Rack
7 module Session
8 # Rack::Session::Memcache provides simple cookie based session management.
9 # Session data is stored in memcached. The corresponding session key is
10 # maintained in the cookie.
11 # You may treat Session::Memcache as you would Session::Pool with the
12 # following caveats.
13 #
14 # * Setting :expire_after to 0 would note to the Memcache server to hang
15 # onto the session data until it would drop it according to it's own
16 # specifications. However, the cookie sent to the client would expire
17 # immediately.
18 #
19 # Note that memcache does drop data before it may be listed to expire. For
20 # a full description of behaviour, please see memcache's documentation.
21
22 class Memcache < Abstract::ID
23 attr_reader :mutex, :pool
24 DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \
25 :namespace => 'rack:session',
26 :memcache_server => 'localhost:11211'
27
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
36
37 def generate_sid
38 loop do
39 sid = super
40 break sid unless @pool.get(sid, true)
41 end
42 end
43
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
63
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
87
88 private
89
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
107 end
108 end
109 end