public
Description: OUTDATED mirror of Rack's darcs repository, use github.com/chneukirchen/rack
Homepage: http://rack.rubyforge.org/
Clone URL: git://github.com/chneukirchen/rack-mirror.git
rack-mirror / lib / rack / session / memcache.rb
100644 84 lines (76 sloc) 2.733 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# AUTHOR: blink <blinketje@gmail.com>; blink#ruby-lang@irc.freenode.net
 
require 'rack/session/abstract/id'
require 'memcache'
 
module Rack
  module Session
    # 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 differences.
    #
    # * 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.
    #
    # Note that memcache does drop data before it may be listed to expire. For
    # a full description of behaviour, please see memcache's documentation.
 
    class Memcache < Abstract::ID
      attr_reader :mutex, :pool
      DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge({
        :namespace => 'rack:session',
        :memcache_server => 'localhost:11211'
      })
 
      def initialize(app, options={})
        super
        @pool = MemCache.new @default_options[:memcache_server], @default_options
        @mutex = Mutex.new
      end
 
      private
 
      def get_session(env, sid)
        session = sid && @pool.get(sid)
        unless session and session.is_a?(Hash)
          session = {}
          lc = 0
          @mutex.synchronize do
            begin
              raise RuntimeError, 'Unique id finding looping excessively' if (lc+=1) > 1000
              sid = "%08x" % rand(0xffffffff)
              ret = @pool.add(sid, session)
            end until /^STORED/ =~ ret
          end
        end
        class << session
          @deleted = []
          def delete key
            (@deleted||=[]) << key
            super
          end
        end
        [sid, session]
      end
 
      def set_session(env, sid)
        session = env['rack.session']
        options = env['rack.session.options']
        expiry = options[:expire_after] || 0
        o, s = @mutex.synchronize do
          old_session = @pool.get(sid)
          unless old_session.is_a?(Hash)
            warn 'Session not properly initialized.' if $DEBUG
            old_session = {}
            @pool.add sid, old_session, expiry
          end
          session.instance_eval do
            @deleted.each{|k| old_session.delete(k) } if defined? @deleted
          end
          @pool.set sid, old_session.merge(session), expiry
          [old_session, session]
        end
        s.each do |k,v|
          next unless o.has_key?(k) and v != o[k]
          warn "session value assignment collision at #{k}: #{o[k]} <- #{v}"
        end if $DEBUG and env['rack.multithread']
      end
    end
  end
end