Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3571: automatic_expiry_for_memcached_sessions.patch

File automatic_expiry_for_memcached_sessions.patch, 3.0 kB (added by skaes, 3 years ago)
  • actionpack/lib/action_controller/session/mem_cache_store.rb

    old new  
    2525 
    2626        # Create a new CGI::Session::MemCache instance 
    2727        # 
    28         # This constructor is used internally by CGI::Session.  The 
     28        # This constructor is used internally by CGI::Session. The 
    2929        # user does not generally need to call it directly. 
    3030        # 
    3131        # +session+ is the session for which this instance is being 
    32         # created.  The session id must only contain alphanumeric 
     32        # created. The session id must only contain alphanumeric 
    3333        # characters; automatically generated session ids observe 
    3434        # this requirement. 
    3535        # 
    36         # +option+ is a hash of options for the initializer. The 
     36        # +options+ is a hash of options for the initializer. The 
    3737        # following options are recognized: 
    3838        # 
    3939        # cache::  an instance of a MemCache client to use as the 
    4040        #      session cache. 
    4141        # 
     42        # expires:: an expiry time value to use for session entries in 
     43        #     the session cache. +expires+ is interpreted in seconds 
     44        #     relative to the current time if itÂ’s less than 60*60*24*30 
     45        #     (30 days), or as an absolute Unix time (e.g., Time#to_i) if 
     46        #     greater. If +expires+ is +0+, or not passed on +options+, 
     47        #     the entry will never expire. 
     48        # 
    4249        # This session's memcache entry will be created if it does 
    4350        # not exist, or retrieved if it does. 
    4451        def initialize(session, options = {}) 
    4552          id = session.session_id 
    4653          unless check_id(id) 
    4754            raise ArgumentError, "session_id '%s' is invalid" % id 
    48              end 
     55          end 
    4956          @cache = options['cache'] || MemCache.new('localhost') 
    50               @session_key = "session:#{id}" 
    51               @hash = {} 
     57          @expires = options['expires'] || 0 
     58          @session_key = "session:#{id}" 
     59          @session_data = {} 
    5260        end 
    5361 
    5462        # Restore session state from the session's memcache entry. 
     
    5664        # Returns the session state as a hash. 
    5765        def restore 
    5866          begin 
    59             @hash = @cache[@session_key] 
     67            @session_data = @cache[@session_key] || {} 
    6068          rescue 
    61             # Ignore session get failures. 
     69            @session_data = {} 
    6270          end 
    63           @hash = {} unless @hash 
    64               @hash 
    6571        end 
    6672 
    6773        # Save session state to the session's memcache entry. 
    6874        def update 
    6975          begin 
    70             @cache[@session_key] = @hash 
     76            @cache.set(@session_key, @session_data, @expires) 
    7177          rescue 
    7278            # Ignore session update failures. 
    7379          end 
     
    8591          rescue 
    8692            # Ignore session delete failures. 
    8793          end 
    88              @hash = {} 
     94          @session_data = {} 
    8995        end 
    9096      end 
    9197    end