unify internal cache mechanism

This commit is contained in:
Thomas Reynolds 2011-11-19 18:53:18 -08:00
parent a06eb91f45
commit 425bd603f5
5 changed files with 70 additions and 56 deletions

View file

@ -59,7 +59,7 @@ $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
module Middleman module Middleman
# Auto-load modules on-demand # Auto-load modules on-demand
autoload :Base, "middleman/base" autoload :Base, "middleman/base"
autoload :Hooks, "middleman/hooks" autoload :Cache, "middleman/cache"
autoload :Builder, "middleman/builder" autoload :Builder, "middleman/builder"
autoload :CLI, "middleman/cli" autoload :CLI, "middleman/cli"
autoload :Templates, "middleman/templates" autoload :Templates, "middleman/templates"
@ -70,7 +70,6 @@ module Middleman
autoload :Sass, "middleman/renderers/sass" autoload :Sass, "middleman/renderers/sass"
autoload :Markdown, "middleman/renderers/markdown" autoload :Markdown, "middleman/renderers/markdown"
autoload :ERb, "middleman/renderers/erb" autoload :ERb, "middleman/renderers/erb"
autoload :CoffeeScript, "middleman/renderers/coffee_script"
autoload :Liquid, "middleman/renderers/liquid" autoload :Liquid, "middleman/renderers/liquid"
end end

View file

@ -164,6 +164,10 @@ class Middleman::Base
run_hook :initialized run_hook :initialized
end end
def cache
@_cache ||= ::Middleman::Cache.new
end
attr :env attr :env
attr :req attr :req
attr :res attr :res
@ -258,11 +262,11 @@ class Middleman::Base
throw "Could not locate layout: #{local_layout}" unless layout_path throw "Could not locate layout: #{local_layout}" unless layout_path
render(layout_path, locals, options) do internal_render(layout_path, locals, options) do
render(path, locals, options) internal_render(path, locals, options)
end end
else else
render(path, locals) internal_render(path, locals)
end end
content_type mime_type(File.extname(@request_path)) content_type mime_type(File.extname(@request_path))
@ -273,12 +277,9 @@ class Middleman::Base
public public
def extensionless_path(file) def extensionless_path(file)
@_extensionless_path_cache ||= {} cache.fetch(:extensionless_path, file) do
if @_extensionless_path_cache.has_key?(file)
@_extensionless_path_cache[file]
else
path = file.dup path = file.dup
end_of_the_line = false end_of_the_line = false
while !end_of_the_line while !end_of_the_line
if !Tilt[path].nil? if !Tilt[path].nil?
@ -288,7 +289,6 @@ public
end end
end end
@_extensionless_path_cache[file] = path
path path
end end
end end
@ -301,28 +301,14 @@ public
@current_path || nil @current_path || nil
end 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) def full_path(path)
parts = path ? path.split('/') : [] cache.fetch(:full_path, path) do
if parts.last.nil? || parts.last.split('.').length == 1 parts = path ? path.split('/') : []
path = File.join(path, index_file) if parts.last.nil? || parts.last.split('.').length == 1
path = File.join(path, index_file)
end
"/" + path.sub(%r{^/}, '')
end end
"/" + path.sub(%r{^/}, '')
end end
def not_found def not_found
@ -333,8 +319,7 @@ public
def resolve_template(request_path, options={}) def resolve_template(request_path, options={})
request_path = request_path.to_s request_path = request_path.to_s
@_resolved_templates ||= {} cache.fetch(:resolve_template, request_path, options) do
if !@_resolved_templates.has_key?(request_path)
relative_path = request_path.sub(%r{^/}, "") relative_path = request_path.sub(%r{^/}, "")
on_disk_path = File.expand_path(relative_path, source_dir) on_disk_path = File.expand_path(relative_path, source_dir)
@ -366,10 +351,8 @@ public
false false
end end
@_resolved_templates[request_path] = result result
end end
@_resolved_templates[request_path]
end end
def send_file(path) def send_file(path)
@ -382,33 +365,24 @@ public
halt file.serving(env) halt file.serving(env)
end end
def render(path, locals = {}, options = {}, &block) # Sinatra render method signature
path = path.to_s def render(engine, data, options={}, locals={}, &block)
internal_render(data, locals, options, &block)
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)
end end
def options_for_ext(ext) def options_for_ext(ext)
@_options_for_ext_cache ||= {} cache.fetch(:options_for_ext, ext) do
if !@_options_for_ext_cache.has_key?(ext)
options = {} options = {}
extension_class = Tilt[ext] extension_class = Tilt[ext]
matched_exts = []
Tilt.mappings.each do |ext, engines| Tilt.mappings.each do |ext, engines|
next unless engines.include? extension_class next unless engines.include? extension_class
engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {} engine_options = respond_to?(ext.to_sym) ? send(ext.to_sym) : {}
options.merge!(engine_options) options.merge!(engine_options)
end end
@_options_for_ext_cache[ext] = options options
end end
@_options_for_ext_cache[ext]
end end
def mime_type(type, value=nil) def mime_type(type, value=nil)
@ -435,6 +409,7 @@ public
res['Content-Type'] = mime_type res['Content-Type'] = mime_type
end end
# Forward to class level
def helpers(*extensions, &block) def helpers(*extensions, &block)
self.class.helpers(*extensions, &block) self.class.helpers(*extensions, &block)
end end
@ -446,4 +421,21 @@ public
def map(map, &block) def map(map, &block)
self.class.map(map, &block) self.class.map(map, &block)
end 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 end

View file

@ -16,7 +16,7 @@ module Middleman
request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "") request_path = destination.sub(/^#{SHARED_SERVER_INST.build_dir}/, "")
# begin begin
destination, request_path = SHARED_SERVER_INST.reroute_builder(destination, request_path) destination, request_path = SHARED_SERVER_INST.reroute_builder(destination, request_path)
request_path.gsub!(/\s/, "%20") request_path.gsub!(/\s/, "%20")
@ -25,9 +25,9 @@ module Middleman
create_file destination, nil, config do create_file destination, nil, config do
response.body response.body
end if response.status == 200 end if response.status == 200
# rescue rescue
# say_status :error, destination, :red say_status :error, destination, :red
# end end
end end
end end

23
lib/middleman/cache.rb Normal file
View file

@ -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

View file

@ -77,7 +77,7 @@ module Middleman::CoreExtensions::FrontMatter
file = file.sub(@app.source_dir, "") file = file.sub(@app.source_dir, "")
@local_data[file] = result @local_data[file] = result
path = File.join(@app.source_dir, file) path = File.join(@app.source_dir, file)
@app.raw_templates_cache[path] = result[1] @app.cache.set([:raw_template, path], result[1])
end end
end end