Quick cache for burst reads.
This commit is contained in:
parent
e1130066f2
commit
28a77171e3
@ -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|
|
||||
|
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