Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache contents of included file read into memory #8104

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/jekyll/site.rb
Expand Up @@ -83,7 +83,6 @@ def print_stats
Jekyll.logger.info @liquid_renderer.stats_table
end

# rubocop:disable Metrics/MethodLength
#
# Reset Site details.
#
Expand All @@ -103,17 +102,14 @@ def reset
@collections = nil
@documents = nil
@docs_to_write = nil
@regenerator.clear_cache
@liquid_renderer.reset
@site_cleaner = nil
frontmatter_defaults.reset

reset_caches
raise ArgumentError, "limit_posts must be a non-negative number" if limit_posts.negative?

Jekyll::Cache.clear_if_config_changed config
Jekyll::Hooks.trigger :site, :after_reset, self
end
# rubocop:enable Metrics/MethodLength

# Load necessary libraries, plugins, converters, and generators.
#
Expand Down Expand Up @@ -467,6 +463,13 @@ def site_cleaner
@site_cleaner ||= Cleaner.new(self)
end

def reset_caches
@regenerator.clear_cache
@liquid_renderer.reset
frontmatter_defaults.reset
Jekyll::Tags::IncludeTag.reset_contents_cache
end

# Disable Marshaling cache to disk in Safe Mode
def configure_cache
Jekyll::Cache.cache_dir = in_source_dir(config["cache_dir"], "Jekyll/Cache")
Expand Down
13 changes: 12 additions & 1 deletion lib/jekyll/tags/include.rb
Expand Up @@ -176,7 +176,18 @@ def realpath_prefixed_with?(path, dir)

# This method allows to modify the file content by inheriting from the class.
def read_file(file, context)
File.read(file, **file_read_opts(context))
self.class.contents_cache[file] ||= begin
Jekyll.logger.debug "Reading Include:", file.cyan
File.read(file, **file_read_opts(context))
end
end

def self.contents_cache
@contents_cache ||= {}
end

def self.reset_contents_cache
@contents_cache = nil
end

private
Expand Down