lib/interlock/config.rb in interlock-1.3 vs lib/interlock/config.rb in interlock-1.4

- old
+ new

@@ -1,23 +1,36 @@ - +#require 'ehcache' module Interlock DEFAULTS = { :ttl => 1.day, :namespace => 'app', :servers => ['127.0.0.1:11211'], :client => 'memcache-client', :with_finders => false } - CLIENT_KEYS = [ + CLIENT_KEYS =[ + :prefix_key, + :distribution, + :verify_key, + :tcp_nodelay, :hash, - :no_block, + :hash_with_prefix_key, + :show_backtraces, + :default_ttl, + :ketama_weighted, + :retry_timeout, + :default_weight, :buffer_requests, + :timeout, + :sort_hosts, + :cache_lookups, + :connect_timeout, + :no_block, + :failover, :support_cas, - :tcp_nodelay, - :distribution, :namespace ] mattr_accessor :config @@config = DEFAULTS @@ -40,42 +53,53 @@ config.deep_symbolize_keys! Interlock.config.merge!(config[:defaults] || {}) Interlock.config.merge!(config[RAILS_ENV.to_sym] || {}) end - + install_memcached install_fragments + + # Always install the finders. install_finders if Interlock.config[:with_finders] end # # Configure memcached for this app. # def install_memcached Interlock.config[:namespace] << "-#{RAILS_ENV}" unless defined? Object::CACHE - + # Give people a choice of client, even though I don't like conditional dependencies. klass = case Interlock.config[:client] when 'memcached' - Memcached::Rails + begin + Memcached::Rails + rescue ArgumentError + raise ConfigurationError, "'memcached' client requested but not installed. Try 'sudo gem install memcached'." + end + when 'memcache-client' - raise ConfigurationError, "You have the Ruby-MemCache gem installed. Please uninstall Ruby-MemCache, or otherwise guarantee that memcache-client will load instead." if MemCache.constants.include?('SVNURL') - MemCache - else - raise ConfigurationError, "Invalid client name '#{Interlock.config[:client]}'" + begin + if MemCache.constants.include?('SVNURL') + raise ConfigurationError, "You have the Ruby-MemCache gem installed. Please uninstall Ruby-MemCache, or otherwise guarantee that memcache-client will load instead." + end + MemCache + rescue ArgumentError + raise ConfigurationError, "'memcache-client' client requested but not installed. Try 'sudo gem install memcache-client'." + end end Object.const_set('CACHE', klass.new( Interlock.config[:servers], Interlock.config.slice(*CLIENT_KEYS) ) ) - + # Mark that we're the ones who did it. class << CACHE def installed_by_interlock; true; end end @@ -102,49 +126,43 @@ def write(name, content, options = {}) set(name.to_s, content, options.is_a?(Hash) ? options[:ttl] : Interlock.config[:ttl] ) - end - end - + end + end end # # Configure Rails to use the memcached store for fragments, and optionally, sessions. # def install_fragments # Memcached fragment caching is mandatory ActionView::Helpers::CacheHelper.class_eval do - def cache(name, options = nil, &block) + def cache(name = {}, options = nil, &block) # Things explode if options does not default to nil RAILS_DEFAULT_LOGGER.debug "** fragment #{name} stored via obsolete cache() call" - @controller.cache_erb_fragment(block, name, options) + @controller.fragment_for(output_buffer, name, options, &block) end end - ActionController::Base.fragment_cache_store = CACHE + ActionController::Base.cache_store = CACHE # Sessions are optional if Interlock.config[:sessions] + # XXX Right now this requires memcache-client to be installed, due to a Rails problem. + # http://dev.rubyonrails.org/ticket/11290 ActionController::Base.session_store = :mem_cache_store ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update 'cache' => CACHE end end # # Configure ActiveRecord#find caching. # def install_finders - # RAILS_DEFAULT_LOGGER.warn "** using interlock finder caches" - class << ActiveRecord::Base - private - alias :find_via_db :find - remove_method :find - - public - include Interlock::Finders - end + RAILS_DEFAULT_LOGGER.warn "** using interlock finder caches" + ActiveRecord::Base.send(:include, Interlock::Finders) end - end + end end -end \ No newline at end of file +end