Enable "cache" option in _config.yml

Fixes #66
This commit is contained in:
Joseph Wynn 2017-07-16 21:19:43 +01:00
parent b1ac375f60
commit aba1d58246
4 changed files with 36 additions and 5 deletions

View File

@ -75,6 +75,11 @@ responsive_image:
# Whether or not to save the generated assets into the source folder. # Whether or not to save the generated assets into the source folder.
save_to_source: false save_to_source: false
# [Optional, Default: false]
# Cache the result of {% responsive_image %} and {% responsive_image_block %}
# tags. See the "Caching" section of the README for more information.
cache: false
# [Optional, Default: []] # [Optional, Default: []]
# By default, only images referenced by the responsive_image and responsive_image_block # By default, only images referenced by the responsive_image and responsive_image_block
# tags are resized. Here you can set a list of paths or path globs to resize other # tags are resized. Here you can set a list of paths or path globs to resize other
@ -213,3 +218,28 @@ Image objects (like `original` and each object in `resized`) contain the followi
| `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). | | `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). | | `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). | | `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |
### Caching
You may be able to speed up the build of large sites by enabling render caching. This is usually only effective when the same image is used many times, for example a header image that is rendered in every post.
The recommended way to enable caching is on an image-by-image basis, by adding `cache: true` to the tag:
```twig
{% responsive_image path: 'assets/my-file.jpg' cache: true %}
{% responsive_image_block %}
path: assets/my-file.jpg
cache: true
{% endresponsive_image_block %}
```
You can also enable it for all images by adding `cache: true` to your `_config.yml`:
```yaml
responsive_image:
cache: true
template: _includes/responsive-image.html
sizes:
- ...
```

View File

@ -8,7 +8,8 @@ module Jekyll
'sizes' => [], 'sizes' => [],
'extra_images' => [], 'extra_images' => [],
'auto_rotate' => false, 'auto_rotate' => false,
'save_to_source' => true 'save_to_source' => true,
'cache' => false
} }
def initialize(site) def initialize(site)

View File

@ -9,12 +9,12 @@ module Jekyll
end end
def render_responsive_image def render_responsive_image
config = Config.new(@site).to_h
use_cache = config['cache'] || @attributes['cache']
cache_key = @attributes.to_s cache_key = @attributes.to_s
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil result = use_cache ? RenderCache.get(cache_key) : nil
if result.nil? if result.nil?
config = Config.new(@site).to_h
image = ImageProcessor.process(@attributes['path'], config) image = ImageProcessor.process(@attributes['path'], config)
@attributes['original'] = image[:original] @attributes['original'] = image[:original]
@attributes['resized'] = image[:resized] @attributes['resized'] = image[:resized]

View File

@ -1,5 +1,5 @@
module Jekyll module Jekyll
module ResponsiveImage module ResponsiveImage
VERSION = '1.3.1'.freeze VERSION = '1.4.0'.freeze
end end
end end