Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / session / mem_cache_store.rb
1 begin
2 require_library_or_gem 'memcache'
3
4 module ActionController
5 module Session
6 class MemCacheStore < AbstractStore
7 def initialize(app, options = {})
8 # Support old :expires option
9 options[:expire_after] ||= options[:expires]
10
11 super
12
13 @default_options = {
14 :namespace => 'rack:session',
15 :memcache_server => 'localhost:11211'
16 }.merge(@default_options)
17
18 @pool = options[:cache] || MemCache.new(@default_options[:memcache_server], @default_options)
19 unless @pool.servers.any? { |s| s.alive? }
20 raise "#{self} unable to find server during initialization."
21 end
22 @mutex = Mutex.new
23
24 super
25 end
26
27 private
28 def get_session(env, sid)
29 sid ||= generate_sid
30 begin
31 session = @pool.get(sid) || {}
32 rescue MemCache::MemCacheError, Errno::ECONNREFUSED
33 session = {}
34 end
35 [sid, session]
36 end
37
38 def set_session(env, sid, session_data)
39 options = env['rack.session.options']
40 expiry = options[:expire_after] || 0
41 @pool.set(sid, session_data, expiry)
42 return true
43 rescue MemCache::MemCacheError, Errno::ECONNREFUSED
44 return false
45 end
46 end
47 end
48 end
49 rescue LoadError
50 # MemCache wasn't available so neither can the store be
51 end