Quick cache for burst reads.

This commit is contained in:
Chris Eppstein 2010-11-29 14:33:57 -08:00
parent e25508f336
commit c55896b493
2 changed files with 17 additions and 1 deletions

View File

@ -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|

View File

@ -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