Make partial_cache a configurable option

This commit is contained in:
Ian Ker-Seymer
2024-09-23 15:40:49 -04:00
parent b233b3d081
commit 62c9cd88e1
6 changed files with 25 additions and 10 deletions
+2 -1
View File
@@ -19,7 +19,7 @@ module Liquid
# rubocop:disable Metrics/ParameterLists
def self.build(environment: Environment.default, environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_environments: {}, &block)
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, &block)
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, environment, &block)
end
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_environments = {}, environment = Environment.default)
@@ -41,6 +41,7 @@ module Liquid
@disabled_tags = {}
@registers.static[:cached_partials] ||= {}
@registers.static[:partial_cache] ||= environment.partial_cache
@registers.static[:file_system] ||= environment.file_system
@registers.static[:template_factory] ||= Liquid::TemplateFactory.new
+6 -1
View File
@@ -26,6 +26,9 @@ module Liquid
# template can consume.
attr_accessor :default_resource_limits
# Implementation for partial caching must respond to `load`
attr_accessor :partial_cache
class << self
# Creates a new environment instance.
#
@@ -39,12 +42,13 @@ module Liquid
# render exceptions.
# @yieldparam environment [Environment] The environment instance that is being built.
# @return [Environment] The new environment instance.
def build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil)
def build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil, partial_cache: nil)
ret = new
ret.tags = Template::TagRegistry.new(tags) if tags
ret.file_system = file_system if file_system
ret.error_mode = error_mode if error_mode
ret.exception_renderer = exception_renderer if exception_renderer
ret.partial_cache = partial_cache if partial_cache
yield ret if block_given?
ret.freeze
end
@@ -83,6 +87,7 @@ module Liquid
@file_system = BlankFileSystem.new
@default_resource_limits = Const::EMPTY_HASH
@strainer_template_class_cache = {}
@partial_cache = PartialCache
end
# Registers a new tag with the environment.
+3 -1
View File
@@ -54,7 +54,9 @@ module Liquid
template_name = context.evaluate(@template_name_expr)
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name.is_a?(String)
partial = PartialCache.load(
partial_cache = context.registers[:partial_cache]
partial = partial_cache.load(
template_name,
context: context,
parse_context: parse_context,
+2 -1
View File
@@ -66,7 +66,8 @@ module Liquid
template_name = @template_name_expr
raise ::ArgumentError unless template_name.is_a?(String)
partial = PartialCache.load(
partial_cache = context.registers[:partial_cache]
partial = partial_cache.load(
template_name,
context: context,
parse_context: parse_context,