Quick cache for burst reads.
This commit is contained in:
parent
e25508f336
commit
c55896b493
@ -1,7 +1,7 @@
|
|||||||
module Compass
|
module Compass
|
||||||
end
|
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}"
|
require "compass/#{lib}"
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -13,6 +13,7 @@ module Compass
|
|||||||
File.expand_path(File.join(File.dirname(__FILE__)))
|
File.expand_path(File.join(File.dirname(__FILE__)))
|
||||||
end
|
end
|
||||||
module_function :base_directory, :lib_directory
|
module_function :base_directory, :lib_directory
|
||||||
|
extend QuickCache
|
||||||
end
|
end
|
||||||
|
|
||||||
%w(configuration frameworks app_integration actions compiler sprites).each do |lib|
|
%w(configuration frameworks app_integration actions compiler sprites).each do |lib|
|
||||||
|
15
lib/compass/quick_cache.rb
Normal file
15
lib/compass/quick_cache.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user