From 425bd603f5e1ab185d59e0f94b14c0e3c99c6eb4 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sat, 19 Nov 2011 18:53:18 -0800 Subject: [PATCH] unify internal cache mechanism --- lib/middleman.rb | 3 +- lib/middleman/base.rb | 90 +++++++++---------- lib/middleman/builder.rb | 8 +- lib/middleman/cache.rb | 23 +++++ lib/middleman/core_extensions/front_matter.rb | 2 +- 5 files changed, 70 insertions(+), 56 deletions(-) create mode 100644 lib/middleman/cache.rb diff --git a/lib/middleman.rb b/lib/middleman.rb index ebf32a3e..80ee697d 100755 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -59,7 +59,7 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) module Middleman # Auto-load modules on-demand autoload :Base, "middleman/base" - autoload :Hooks, "middleman/hooks" + autoload :Cache, "middleman/cache" autoload :Builder, "middleman/builder" autoload :CLI, "middleman/cli" autoload :Templates, "middleman/templates" @@ -70,7 +70,6 @@ module Middleman autoload :Sass, "middleman/renderers/sass" autoload :Markdown, "middleman/renderers/markdown" autoload :ERb, "middleman/renderers/erb" - autoload :CoffeeScript, "middleman/renderers/coffee_script" autoload :Liquid, "middleman/renderers/liquid" end diff --git a/lib/middleman/base.rb b/lib/middleman/base.rb index b086f369..62975da8 100644 --- a/lib/middleman/base.rb +++ b/lib/middleman/base.rb @@ -164,6 +164,10 @@ class Middleman::Base run_hook :initialized end + def cache + @_cache ||= ::Middleman::Cache.new + end + attr :env attr :req attr :res @@ -258,11 +262,11 @@ class Middleman::Base throw "Could not locate layout: #{local_layout}" unless layout_path - render(layout_path, locals, options) do - render(path, locals, options) + internal_render(layout_path, locals, options) do + internal_render(path, locals, options) end else - render(path, locals) + internal_render(path, locals) end content_type mime_type(File.extname(@request_path)) @@ -273,12 +277,9 @@ class Middleman::Base public def extensionless_path(file) - @_extensionless_path_cache ||= {} - - if @_extensionless_path_cache.has_key?(file) - @_extensionless_path_cache[file] - else + cache.fetch(:extensionless_path, file) do path = file.dup + end_of_the_line = false while !end_of_the_line if !Tilt[path].nil? @@ -288,7 +289,6 @@ public end end - @_extensionless_path_cache[file] = path path end end @@ -301,28 +301,14 @@ public @current_path || nil end - def raw_templates_cache - @_raw_templates_cache ||= {} - end - - def read_raw_template(path) - if raw_templates_cache.has_key?(path) - raw_templates_cache[path] - else - File.read(path) - end - end - - def map(map, &block) - self.class.map(map, &block) - end - def full_path(path) - parts = path ? path.split('/') : [] - if parts.last.nil? || parts.last.split('.').length == 1 - path = File.join(path, index_file) + cache.fetch(:full_path, path) do + parts = path ? path.split('/') : [] + if parts.last.nil? || parts.last.split('.').length == 1 + path = File.join(path, index_file) + end + "/" + path.sub(%r{^/}, '') end - "/" + path.sub(%r{^/}, '') end def not_found @@ -333,8 +319,7 @@ public def resolve_template(request_path, options={}) request_path = request_path.to_s - @_resolved_templates ||= {} - if !@_resolved_templates.has_key?(request_path) + cache.fetch(:resolve_template, request_path, options) do relative_path = request_path.sub(%r{^/}, "") on_disk_path = File.expand_path(relative_path, source_dir) @@ -366,10 +351,8 @@ public false end - @_resolved_templates[request_path] = result + result end - - @_resolved_templates[request_path] end def send_file(path) @@ -382,33 +365,24 @@ public halt file.serving(env) end - def render(path, locals = {}, options = {}, &block) - path = path.to_s - - options.merge!(options_for_ext(File.extname(path))) - - body = read_raw_template(path) - template = ::Tilt.new(path, 1, options) { body } - template.render(self, locals, &block) + # Sinatra render method signature + def render(engine, data, options={}, locals={}, &block) + internal_render(data, locals, options, &block) end def options_for_ext(ext) - @_options_for_ext_cache ||= {} - - if !@_options_for_ext_cache.has_key?(ext) + cache.fetch(:options_for_ext, ext) do options = {} + extension_class = Tilt[ext] - matched_exts = [] Tilt.mappings.each do |ext, engines| next unless engines.include? extension_class engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {} options.merge!(engine_options) end - @_options_for_ext_cache[ext] = options + options end - - @_options_for_ext_cache[ext] end def mime_type(type, value=nil) @@ -435,6 +409,7 @@ public res['Content-Type'] = mime_type end + # Forward to class level def helpers(*extensions, &block) self.class.helpers(*extensions, &block) end @@ -446,4 +421,21 @@ public def map(map, &block) self.class.map(map, &block) end + +private + def internal_render(path, locals = {}, options = {}, &block) + path = path.to_s + + options.merge!(options_for_ext(File.extname(path))) + + body = cache.fetch(:raw_template, path) do + File.read(path) + end + + template = cache.fetch(:compiled_template, options, body) do + ::Tilt.new(path, 1, options) { body } + end + + template.render(self, locals, &block) + end end \ No newline at end of file diff --git a/lib/middleman/builder.rb b/lib/middleman/builder.rb index 876a7496..fed9880c 100644 --- a/lib/middleman/builder.rb +++ b/lib/middleman/builder.rb @@ -16,7 +16,7 @@ module Middleman request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "") - # begin + begin destination, request_path = SHARED_SERVER_INST.reroute_builder(destination, request_path) request_path.gsub!(/\s/, "%20") @@ -25,9 +25,9 @@ module Middleman create_file destination, nil, config do response.body end if response.status == 200 - # rescue - # say_status :error, destination, :red - # end + rescue + say_status :error, destination, :red + end end end diff --git a/lib/middleman/cache.rb b/lib/middleman/cache.rb new file mode 100644 index 00000000..dd6f3922 --- /dev/null +++ b/lib/middleman/cache.rb @@ -0,0 +1,23 @@ +module Middleman + class Cache + def initialize + @cache = {} + end + + def fetch(*key) + @cache[key] ||= yield + end + + def clear + @cache = {} + end + + def set(key, value) + @cache[key] = value + end + + def remove(*key) + @cache.delete(key) + end + end +end \ No newline at end of file diff --git a/lib/middleman/core_extensions/front_matter.rb b/lib/middleman/core_extensions/front_matter.rb index e86af699..bdda4c7b 100644 --- a/lib/middleman/core_extensions/front_matter.rb +++ b/lib/middleman/core_extensions/front_matter.rb @@ -77,7 +77,7 @@ module Middleman::CoreExtensions::FrontMatter file = file.sub(@app.source_dir, "") @local_data[file] = result path = File.join(@app.source_dir, file) - @app.raw_templates_cache[path] = result[1] + @app.cache.set([:raw_template, path], result[1]) end end