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,6 +29,7 @@ end
def reference_item(options) def reference_item(options)
stylesheet = options[:stylesheet] stylesheet = options[:stylesheet]
@site.cached("reference/item/#{stylesheet}") do
path = stylesheet_path(stylesheet) path = stylesheet_path(stylesheet)
if path if path
@items.detect do |i| @items.detect do |i|
@ -38,6 +38,7 @@ def reference_item(options)
end end
end end
end end
end
def reference_path(options) def reference_path(options)
if item = reference_item(options) if item = reference_item(options)
@ -57,6 +58,7 @@ def import_paths
end end
def stylesheet_path(ss) def stylesheet_path(ss)
@site.cached("stylesheet/path/#{ss}") do
possible_filenames_for_stylesheet(ss).each do |filename| possible_filenames_for_stylesheet(ss).each do |filename|
import_paths.each do |import_path| import_paths.each do |import_path|
full_path = File.join(import_path.first, filename) full_path = File.join(import_path.first, filename)
@ -66,6 +68,7 @@ def stylesheet_path(ss)
end end
end end
end end
end
def possible_filenames_for_stylesheet(ss) def possible_filenames_for_stylesheet(ss)
ext = File.extname(ss) ext = File.extname(ss)
@ -132,16 +135,19 @@ 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
def examples(item, mixin = nil) def examples(item, mixin = nil)
examples = examples_for_item(item) examples = examples_for_item(item)