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)
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
# @return [String]
def render(*args, &block)
@ -250,6 +238,26 @@ module Middleman::Sitemap
return [] unless parent
parent.children.reject { |p| p == self }
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

View file

@ -31,22 +31,10 @@ module Middleman::Sitemap
# Simple aliases
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
# @return [Hash]
def metadata
metadata = app.cache.fetch(:metadata, source_file) do
metadata = @page.cache.fetch(:metadata) do
data = { :options => {}, :locals => {}, :page => {}, :blocks => [] }
app.provides_metadata.each do |callback, matcher|
@ -104,4 +92,4 @@ module Middleman::Sitemap
result
end
end
end
end

View file

@ -8,7 +8,7 @@ module Middleman::Extensions
app.after_configuration do
sitemap.reroute do |destination, page|
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]
destination.sub(/\.(\w+)$/) { |ext| "-#{digest}#{ext}" }
end