Merge pull request #12 from wildlyinaccurate/experimental-result-caching

[Experimental] enable internal caching of responsive_image tag results
This commit is contained in:
Joseph Wynn 2016-02-02 20:52:31 +00:00
commit b117107385
4 changed files with 60 additions and 19 deletions

View File

@ -7,6 +7,7 @@ require 'rmagick'
require 'jekyll/responsive_image/version'
require 'jekyll/responsive_image/defaults'
require 'jekyll/responsive_image/utils'
require 'jekyll/responsive_image/render_cache'
require 'jekyll/responsive_image/image_processor'
require 'jekyll/responsive_image/resize_handler'
require 'jekyll/responsive_image/common'

View File

@ -4,20 +4,30 @@ module Jekyll
include Jekyll::ResponsiveImage::Common
def render(context)
site = context.registers[:site]
config = make_config(site)
attributes = YAML.load(super)
image_template = attributes['template'] || config['template']
image = ImageProcessor.process(attributes['path'], config)
attributes['original'] = image[:original]
attributes['resized'] = image[:resized]
cache_key = attributes.to_s
result = attributes['cache'] ? RenderCache.get(cache_key) : nil
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
if result.nil?
site = context.registers[:site]
config = make_config(site)
template.render!(attributes.merge(site.site_payload))
image_template = attributes['template'] || config['template']
image = ImageProcessor.process(attributes['path'], config)
attributes['original'] = image[:original]
attributes['resized'] = image[:resized]
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
result = template.render!(attributes.merge(site.site_payload))
RenderCache.set(cache_key, result)
end
result
end
end
end

View File

@ -0,0 +1,21 @@
module Jekyll
class ResponsiveImage
class RenderCache
attr_accessor :store
class << self
def store
@store ||= {}
end
def get(key)
store[key]
end
def set(key, val)
store[key] = val
end
end
end
end
end

View File

@ -15,19 +15,28 @@ module Jekyll
end
def render(context)
site = context.registers[:site]
config = make_config(site)
cache_key = @attributes.to_s
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil
image = ImageProcessor.process(@attributes['path'], config)
@attributes['original'] = image[:original]
@attributes['resized'] = image[:resized]
if result.nil?
site = context.registers[:site]
config = make_config(site)
image_template = @attributes['template'] || config['template']
image = ImageProcessor.process(@attributes['path'], config)
@attributes['original'] = image[:original]
@attributes['resized'] = image[:resized]
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
image_template = @attributes['template'] || config['template']
template.render!(@attributes.merge(site.site_payload))
partial = File.read(image_template)
template = Liquid::Template.parse(partial)
result = template.render!(@attributes.merge(site.site_payload))
RenderCache.set(cache_key, result)
end
result
end
end
end