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

Ticket #11290: mem_cache_store.patch

File mem_cache_store.patch, 6.3 kB (added by evan, 4 months ago)

Patch for #11290

  • lib/action_controller/session/mem_cache_store.rb

    old new  
    33# == Overview 
    44# 
    55# This file provides the CGI::Session::MemCache class, which builds 
    6 # persistence of storage data on top of the MemCache library.  See 
    7 # cgi/session.rb for more details on session storage managers. 
     6# persistence of storage data on top of a memcached client library.   
     7# See cgi/session.rb for more details on session storage managers. 
    88# 
    99 
    10 begin 
    11   require 'cgi/session' 
    12   require_library_or_gem 'memcache' 
     10require 'cgi/session' 
    1311 
    14   class CGI 
    15     class Session 
    16       # MemCache-based session storage class. 
     12class CGI 
     13  class Session 
     14    # Memcached-based session storage class. 
     15    # 
     16    # This delegates to a toplevel MemCache class provided by a 
     17    # memcached client library.  Session data is marshalled and  
     18    # stored in a remote memcached server. 
     19    class MemCacheStore 
     20      def check_id(id) #:nodoc:# 
     21        /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true 
     22      end 
     23 
     24      # Create a new CGI::Session::MemCache instance 
    1725      # 
    18       # This builds upon the top-level MemCache class provided by the 
    19       # library file memcache.rb.  Session data is marshalled and stored 
    20       # in a memcached cache. 
    21       class MemCacheStore 
    22         def check_id(id) #:nodoc:# 
    23           /[^0-9a-zA-Z]+/ =~ id.to_s ? false : true 
     26      # This constructor is used internally by CGI::Session. The 
     27      # user does not generally need to call it directly. 
     28      # 
     29      # +session+ is the session for which this instance is being 
     30      # created. The session id must only contain alphanumeric 
     31      # characters; automatically generated session ids observe 
     32      # this requirement. 
     33      # 
     34      # +options+ is a hash of options for the initializer. The 
     35      # following options are recognized: 
     36      # 
     37      # cache::  an instance of a MemCache client to use as the 
     38      #      session cache. 
     39      # 
     40      # expires:: an expiry time value to use for session entries in 
     41      #     the session cache. +expires+ is interpreted in seconds 
     42      #     relative to the current time if it’s less than 60*60*24*30 
     43      #     (30 days), or as an absolute Unix time (e.g., Time#to_i) if 
     44      #     greater. If +expires+ is +0+, or not passed on +options+, 
     45      #     the entry will never expire. 
     46      # 
     47      # This session's memcache entry will be created if it does 
     48      # not exist, or retrieved if it does. 
     49      def initialize(session, options = {}) 
     50        id = session.session_id 
     51        unless check_id(id) 
     52          raise ArgumentError, "session_id '%s' is invalid" % id 
    2453        end 
    25  
    26         # Create a new CGI::Session::MemCache instance 
    27         # 
    28         # This constructor is used internally by CGI::Session. The 
    29         # user does not generally need to call it directly. 
    30         # 
    31         # +session+ is the session for which this instance is being 
    32         # created. The session id must only contain alphanumeric 
    33         # characters; automatically generated session ids observe 
    34         # this requirement. 
    35         # 
    36         # +options+ is a hash of options for the initializer. The 
    37         # following options are recognized: 
    38         # 
    39         # cache::  an instance of a MemCache client to use as the 
    40         #      session cache. 
    41         # 
    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         # 
    49         # This session's memcache entry will be created if it does 
    50         # not exist, or retrieved if it does. 
    51         def initialize(session, options = {}) 
    52           id = session.session_id 
    53           unless check_id(id) 
    54             raise ArgumentError, "session_id '%s' is invalid" % id 
    55           end 
    56           @cache = options['cache'] || MemCache.new('localhost') 
    57           @expires = options['expires'] || 0 
    58           @session_key = "session:#{id}" 
    59           @session_data = {} 
    60           # Add this key to the store if haven't done so yet 
    61           unless @cache.get(@session_key) 
    62             @cache.add(@session_key, @session_data, @expires) 
    63           end 
     54        @cache = options['cache'] 
     55        @expires = options['expires'] || 0 
     56        @session_key = "session:#{id}" 
     57        @session_data = {} 
     58        # Add this key to the store if haven't done so yet 
     59        unless @cache.get(@session_key) 
     60          @cache.add(@session_key, @session_data, @expires) 
    6461        end 
     62      end 
    6563 
    66         # Restore session state from the session's memcache entry. 
    67        
    68         # Returns the session state as a hash. 
    69         def restore 
    70           @session_data = @cache[@session_key] || {} 
    71         end 
     64      # Restore session state from the session's memcache entry. 
     65     
     66      # Returns the session state as a hash. 
     67      def restore 
     68        @session_data = @cache[@session_key] || {} 
     69      end 
    7270 
    73         # Save session state to the session's memcache entry. 
    74         def update 
    75           @cache.set(@session_key, @session_data, @expires) 
    76         end 
    77        
    78         # Update and close the session's memcache entry. 
    79         def close 
    80           update 
    81         end 
     71      # Save session state to the session's memcache entry. 
     72      def update 
     73        @cache.set(@session_key, @session_data, @expires) 
     74      end 
     75     
     76      # Update and close the session's memcache entry. 
     77      def close 
     78        update 
     79      end 
    8280 
    83         # Delete the session's memcache entry. 
    84         def delete 
    85           @cache.delete(@session_key) 
    86           @session_data = {} 
    87         end 
    88          
    89         def data 
    90           @session_data 
    91         end 
    92  
     81      # Delete the session's memcache entry. 
     82      def delete 
     83        @cache.delete(@session_key) 
     84        @session_data = {} 
    9385      end 
     86       
     87      def data 
     88        @session_data 
     89      end 
     90 
    9491    end 
    9592  end 
    96 rescue LoadError 
    97   # MemCache wasn't available so neither can the store be 
    9893end