Merge pull request #335 from bhollis/cache

Introduce a cache tied to individual sitemap pages
This commit is contained in:
Ben Hollis 2012-04-03 09:28:37 -07:00
commit 30c41df909
3 changed files with 23 additions and 27 deletions

View file

@ -113,18 +113,6 @@ module Middleman::Sitemap
store.ignore(self.path) store.ignore(self.path)
end end
# If this is a template, refresh contents
# @return [void]
def touch
template.touch if template?
end
# If this is a template, remove contents
# @return [void]
def delete
template.delete if template?
end
# Render this page # Render this page
# @return [String] # @return [String]
def render(*args, &block) def render(*args, &block)
@ -251,6 +239,26 @@ module Middleman::Sitemap
parent.children.reject { |p| p == self } parent.children.reject { |p| p == self }
end end
# A cache for extensions and internals to use to store
# information about this page. The cache is cleared whenever
# the page changes.
# @return [Middleman::Cache]
def cache
@cache ||= Middleman::Cache.new
end
# Clear out the cache whenever this page changes
# @return [void]
def touch
cache.clear
end
# Clear the cache if the file is deleted
# @return [void]
def delete
cache.clear
end
protected protected
# This page's stored app # This page's stored app

View file

@ -31,22 +31,10 @@ module Middleman::Sitemap
# Simple aliases # Simple aliases
delegate :path, :source_file, :store, :app, :ext, :to => :page delegate :path, :source_file, :store, :app, :ext, :to => :page
# Clear internal frontmatter cache for file if it changes
# @return [void]
def touch
app.cache.remove(:metadata, source_file)
end
# Clear internal frontmatter cache for file if it is deleted
# @return [void]
def delete
app.cache.remove(:metadata, source_file)
end
# Get the metadata for both the current source_file and the current path # Get the metadata for both the current source_file and the current path
# @return [Hash] # @return [Hash]
def metadata def metadata
metadata = app.cache.fetch(:metadata, source_file) do metadata = @page.cache.fetch(:metadata) do
data = { :options => {}, :locals => {}, :page => {}, :blocks => [] } data = { :options => {}, :locals => {}, :page => {}, :blocks => [] }
app.provides_metadata.each do |callback, matcher| app.provides_metadata.each do |callback, matcher|

View file

@ -8,7 +8,7 @@ module Middleman::Extensions
app.after_configuration do app.after_configuration do
sitemap.reroute do |destination, page| sitemap.reroute do |destination, page|
if exts.include? page.ext if exts.include? page.ext
app.cache.fetch(:asset_hash, page.path) do page.cache.fetch(:asset_hash) do
digest = Digest::SHA1.file(page.source_file).hexdigest[0..7] digest = Digest::SHA1.file(page.source_file).hexdigest[0..7]
destination.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" } destination.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end end