Better caching for autocompile.

This commit is contained in:
Chris Eppstein 2010-02-07 09:14:20 -08:00
parent 02570b445b
commit f9a66671f8
2 changed files with 37 additions and 19 deletions

View File

@ -0,0 +1,12 @@
class Nanoc3::Site
def cached(key)
if cache.has_key?(key)
cache[key]
else
cache[key]= yield
end
end
def cache
@cache ||= {}
end
end

View File

@ -4,13 +4,12 @@ def stylesheets_dir(framework)
Compass::Frameworks[framework].stylesheets_directory Compass::Frameworks[framework].stylesheets_directory
end end
def stylesheet_key(item) def tree_key(item)
[item[:framework], item[:stylesheet]].join("/") "tree/"+[item[:framework], item[:stylesheet]].join("/")
end end
def tree(item) def tree(item)
@stylesheets ||= {} @site.cached(tree_key(item)) do
@stylesheets[stylesheet_key(item)] ||= begin
file = File.join(stylesheets_dir(item[:framework]), item[:stylesheet]) file = File.join(stylesheets_dir(item[:framework]), item[:stylesheet])
contents = File.read(file) contents = File.read(file)
Sass::Engine.new(contents).send :to_tree Sass::Engine.new(contents).send :to_tree
@ -30,11 +29,13 @@ end
def reference_item(options) def reference_item(options)
stylesheet = options[:stylesheet] stylesheet = options[:stylesheet]
path = stylesheet_path(stylesheet) @site.cached("reference/item/#{stylesheet}") do
if path path = stylesheet_path(stylesheet)
@items.detect do |i| if path
i[:stylesheet] == path && @items.detect do |i|
i.identifier =~ /^\/reference/ i[:stylesheet] == path &&
i.identifier =~ /^\/reference/
end
end end
end end
end end
@ -57,11 +58,13 @@ def import_paths
end end
def stylesheet_path(ss) def stylesheet_path(ss)
possible_filenames_for_stylesheet(ss).each do |filename| @site.cached("stylesheet/path/#{ss}") do
import_paths.each do |import_path| possible_filenames_for_stylesheet(ss).each do |filename|
full_path = File.join(import_path.first, filename) import_paths.each do |import_path|
if File.exist?(full_path) full_path = File.join(import_path.first, filename)
return "#{import_path.last}#{"/" if import_path.last && import_path.last.length > 0}#{filename}" if File.exist?(full_path)
return "#{import_path.last}#{"/" if import_path.last && import_path.last.length > 0}#{filename}"
end
end end
end end
end end
@ -132,14 +135,17 @@ def mixin_signature(mixin)
end end
def example_items def example_items
@example_items ||= @items.select{|i| i[:example]} @site.cached("examples") do
@items.select{|i| i[:example]}
end
end end
def examples_for_item(item) def examples_for_item(item)
@examples ||= {} @site.cached("examples/#{item.identifier}") do
@examples[item] ||= example_items.select do |i| example_items.select do |i|
i[:framework] == item[:framework] && i[:framework] == item[:framework] &&
i[:stylesheet] == item[:stylesheet] i[:stylesheet] == item[:stylesheet]
end
end end
end end