Skip to content

Commit 7db6d9a

Browse files
committed
Implement aggressive caching
1 parent 4a8a4ea commit 7db6d9a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ If you want an inline display of your photos, I recommend [glightbox](https://gi
4444

4545
at the bottom of the layout.
4646

47+
## Caching
48+
49+
This plugin uses aggressive caching to keep render times short.
50+
If you need to re-render images for any reason, remove the `.jekyll-cache` folder or change the `_config.yml` file.
51+
See the [Cache API tutorial](https://jekyllrb.com/tutorials/cache-api/) for some background.
52+
4753
## Development
4854

4955
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. There is also a test site in `spec/fixtures/test_site` that you can use to try out changes.

lib/cheesy-gallery/base_image_file.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
class CheesyGallery::BaseImageFile < Jekyll::StaticFile
99
extend T::Sig
1010

11+
@@render_cache = T.let(Jekyll::Cache.new('CheesyGallery::Render'), Jekyll::Cache) # don't need to worry about inheritance here # rubocop:disable Style/ClassVars
12+
1113
sig { params(site: Jekyll::Site, collection: Jekyll::Collection, file: Jekyll::StaticFile, dest_path: T.nilable(String)).void }
1214
def initialize(site, collection, file, dest_path = nil)
1315
@source_file = T.let(file, Jekyll::StaticFile)
@@ -27,6 +29,25 @@ def process_and_write(img, path)
2729
img.write(path) {}
2830
end
2931

32+
# Inject cache here to override default delete-before-copy behaviour
33+
# See jekyll:lib/jekyll/static_file.rb for source
34+
def write(dest)
35+
dest_path = destination(dest)
36+
return false if File.exist?(dest_path) && !modified?
37+
38+
self.class.mtimes[path] = mtime
39+
40+
return if @@render_cache.key?("#{dest_path}-rendered") && File.exist?(dest_path)
41+
42+
FileUtils.mkdir_p(File.dirname(dest_path))
43+
FileUtils.rm(dest_path) if File.exist?(dest_path)
44+
copy_file(dest_path)
45+
46+
@@render_cache["#{dest_path}-rendered"] = true
47+
48+
true
49+
end
50+
3051
private
3152

3253
# instead of copying, allow rmagick processing

0 commit comments

Comments
 (0)