From c55896b4935129f96f922de8536e8f5cb59a12ba Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Mon, 29 Nov 2010 14:33:57 -0800 Subject: [PATCH] Quick cache for burst reads. --- lib/compass.rb | 3 ++- lib/compass/quick_cache.rb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 lib/compass/quick_cache.rb diff --git a/lib/compass.rb b/lib/compass.rb index f9375ba0..bb85f181 100644 --- a/lib/compass.rb +++ b/lib/compass.rb @@ -1,7 +1,7 @@ module Compass end -%w(dependencies util sass_extensions core_ext version errors).each do |lib| +%w(dependencies util sass_extensions core_ext version errors quick_cache).each do |lib| require "compass/#{lib}" end @@ -13,6 +13,7 @@ module Compass File.expand_path(File.join(File.dirname(__FILE__))) end module_function :base_directory, :lib_directory + extend QuickCache end %w(configuration frameworks app_integration actions compiler sprites).each do |lib| diff --git a/lib/compass/quick_cache.rb b/lib/compass/quick_cache.rb new file mode 100644 index 00000000..717690e3 --- /dev/null +++ b/lib/compass/quick_cache.rb @@ -0,0 +1,15 @@ +module QuickCache + + # cache a value in memory for just a few seconds + # This can speed up reads of values that change relatively infrequently + # but might be read many times in a short burst of reads. + def quick_cache(key, ttl = 1) + @quick_cache ||= {} + if @quick_cache[key] && @quick_cache[key].first > Time.now - ttl + @quick_cache[key].last + else + (@quick_cache[key] = [Time.now, yield]).last + end + end + +end