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

- old
+ new

@@ -3,22 +3,23 @@ DEFAULTS = { :ttl => 1.day, :namespace => 'app', :servers => ['127.0.0.1:11211'], - :client => 'memcache-client' + :client => 'memcache-client', + :with_finders => false } - CLIENT_KEYS = [ #:nodoc: + CLIENT_KEYS = [ :hash, :no_block, :buffer_requests, :support_cas, :tcp_nodelay, :distribution, :namespace - ] + ] mattr_accessor :config @@config = DEFAULTS module Config @@ -40,18 +41,19 @@ Interlock.config.merge!(config[:defaults] || {}) Interlock.config.merge!(config[RAILS_ENV.to_sym] || {}) end - memcached! - rails! + install_memcached + install_fragments + install_finders if Interlock.config[:with_finders] end # # Configure memcached for this app. # - def memcached! + 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. @@ -93,26 +95,26 @@ # who installed the singleton. class << CACHE include Interlock::Lock def read(*args) - get args.first + get args.first.to_s end def write(name, content, options = {}) - set(name, + set(name.to_s, content, options.is_a?(Hash) ? options[:ttl] : Interlock.config[:ttl] ) end end end # # Configure Rails to use the memcached store for fragments, and optionally, sessions. # - def rails! + def install_fragments # Memcached fragment caching is mandatory ActionView::Helpers::CacheHelper.class_eval do 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" @@ -125,9 +127,24 @@ if Interlock.config[:sessions] 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 + end end end end \ No newline at end of file