aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/cache.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2008-01-03 21:05:12 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2008-01-03 21:05:12 +0000
commit2a9ad9ccbc706e546bf02ec95f864944e7d7983b (patch)
tree868624e91f037840bfbf0aca30bb2ea1c9d78701 /activesupport/lib/active_support/cache.rb
parent288553540b5b2f37497cb19357b25ac12e0498fd (diff)
downloadrails-2a9ad9ccbc706e546bf02ec95f864944e7d7983b.tar.gz
rails-2a9ad9ccbc706e546bf02ec95f864944e7d7983b.tar.bz2
rails-2a9ad9ccbc706e546bf02ec95f864944e7d7983b.zip
Moved the caching stores from ActionController::Caching::Fragments::* to ActiveSupport::Cache::*. If you're explicitly referring to a store, like ActionController::Caching::Fragments::MemoryStore, you need to update that reference with ActiveSupport::Cache::MemoryStore [DHH] Deprecated ActionController::Base.fragment_cache_store for ActionController::Base.cache_store [DHH] All fragment cache keys are now by default prefixed with the 'views/' namespace [DHH] Added ActiveRecord::Base.cache_key to make it easier to cache Active Records in combination with the new ActiveSupport::Cache::* libraries [DHH] Added ActiveSupport::Gzip.decompress/compress(source) as an easy wrapper for Zlib [Tobias Luetke] Included MemCache-Client to make the improved ActiveSupport::Cache::MemCacheStore work out of the box [Bob Cottrell, Eric Hodel] Added config.cache_store to environment options to control the default cache store (default is FileStore if tmp/cache is present, otherwise MemoryStore is used) [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8546 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/cache.rb')
-rw-r--r--activesupport/lib/active_support/cache.rb121
1 files changed, 121 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
new file mode 100644
index 0000000000..8252ada032
--- /dev/null
+++ b/activesupport/lib/active_support/cache.rb
@@ -0,0 +1,121 @@
+module ActiveSupport
+ module Cache
+ def self.lookup_store(*store_option)
+ store, *parameters = *([ store_option ].flatten)
+
+ case store
+ when Symbol
+ store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize)
+ store_class = ActiveSupport::Cache.const_get(store_class_name)
+ store_class.new(*parameters)
+ when nil
+ ActiveSupport::Cache::MemoryStore.new
+ else
+ store
+ end
+ end
+
+ def self.expand_cache_key(key, namespace = nil)
+ expanded_cache_key = namespace ? "#{namespace}/" : ""
+
+ if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
+ expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/"
+ end
+
+ expanded_cache_key << case
+ when key.respond_to?(:cache_key)
+ key.cache_key
+ when key.is_a?(Array)
+ key.collect { |element| expand_cache_key(element) }.to_param
+ when key.respond_to?(:to_param)
+ key.to_param
+ end
+
+ expanded_cache_key
+ end
+
+
+ class Store
+ cattr_accessor :logger
+
+ def initialize
+ end
+
+ def threadsafe!
+ @mutex = Mutex.new
+ self.class.send :include, ThreadSafety
+ self
+ end
+
+ def fetch(key, options = nil)
+ @logger_off = true
+ if value = read(key, options)
+ @logger_off = false
+ log("hit", key, options)
+ value
+ elsif block_given?
+ @logger_off = false
+ log("miss", key, options)
+
+ value = nil
+ seconds = Benchmark.realtime { value = yield }
+
+ @logger_off = true
+ write(key, value, options)
+ @logger_off = false
+
+ log("write (will save #{'%.5f' % seconds})", key, nil)
+
+ value
+ end
+ end
+
+ def read(key, options = nil)
+ log("read", key, options)
+ end
+
+ def write(key, value, options = nil)
+ log("write", key, options)
+ end
+
+ def delete(key, options = nil)
+ log("delete", key, options)
+ end
+
+ def delete_matched(matcher, options = nil)
+ log("delete matched", matcher.inspect, options)
+ end
+
+
+ private
+ def log(operation, key, options)
+ logger.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@logger_off
+ end
+ end
+
+
+ module ThreadSafety #:nodoc:
+ def read(key, options = nil) #:nodoc:
+ @mutex.synchronize { super }
+ end
+
+ def write(key, value, options = nil) #:nodoc:
+ @mutex.synchronize { super }
+ end
+
+ def delete(key, options = nil) #:nodoc:
+ @mutex.synchronize { super }
+ end
+
+ def delete_matched(matcher, options = nil) #:nodoc:
+ @mutex.synchronize { super }
+ end
+ end
+ end
+end
+
+require 'active_support/cache/file_store'
+require 'active_support/cache/memory_store'
+require 'active_support/cache/drb_store'
+require 'active_support/cache/mem_cache_store'
+require 'active_support/cache/compressed_mem_cache_store' \ No newline at end of file