From a07ae905230172a40156cca7c7b2a6becdccb89d Mon Sep 17 00:00:00 2001 From: Michael Go Date: Fri, 10 Jan 2025 15:07:57 -0400 Subject: [PATCH] use Ruby Hash as default expression cache --- Gemfile | 1 + lib/liquid/parse_context.rb | 8 +++-- liquid.gemspec | 1 - test/integration/expression_test.rb | 45 +++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 013b1700..fd9d00b0 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,7 @@ group :benchmark, :test do gem 'benchmark-ips' gem 'memory_profiler' gem 'terminal-table' + gem "lru_redux" install_if -> { RUBY_PLATFORM !~ /mingw|mswin|java/ && RUBY_ENGINE != 'truffleruby' } do gem 'stackprof' diff --git a/lib/liquid/parse_context.rb b/lib/liquid/parse_context.rb index 03052972..60cdf9e4 100644 --- a/lib/liquid/parse_context.rb +++ b/lib/liquid/parse_context.rb @@ -16,8 +16,12 @@ module Liquid # This StringScanner will be shared by all of them @string_scanner = StringScanner.new("") - @expression_cache = if options[:expression_cache] != false - options[:expression_cache] || LruRedux::Cache.new(10_000) + @expression_cache = if options[:expression_cache].nil? + {} + elsif options[:expression_cache].respond_to?(:[]) && options[:expression_cache].respond_to?(:[]=) + options[:expression_cache] + elsif options[:expression_cache] + {} end self.depth = 0 diff --git a/liquid.gemspec b/liquid.gemspec index a58a3601..1b9a9100 100644 --- a/liquid.gemspec +++ b/liquid.gemspec @@ -30,7 +30,6 @@ Gem::Specification.new do |s| s.add_dependency("strscan", ">= 3.1.1") s.add_dependency("bigdecimal") - s.add_dependency("lru_redux") s.add_development_dependency('rake', '~> 13.0') s.add_development_dependency('minitest') diff --git a/test/integration/expression_test.rb b/test/integration/expression_test.rb index 190a6bec..0eef5dc9 100644 --- a/test/integration/expression_test.rb +++ b/test/integration/expression_test.rb @@ -58,6 +58,51 @@ class ExpressionTest < Minitest::Test def test_expression_cache skip("Liquid-C does not support Expression caching") if defined?(Liquid::C) && Liquid::C.enabled + cache = {} + template = <<~LIQUID + {% assign x = 1 %} + {{ x }} + {% assign x = 2 %} + {{ x }} + {% assign y = 1 %} + {{ y }} + LIQUID + + Liquid::Template.parse(template, expression_cache: cache).render + + assert_equal( + ["1", "2", "x", "y"], + cache.to_a.map { _1[0] }.sort, + ) + end + + def test_expression_cache_with_true_boolean + skip("Liquid-C does not support Expression caching") if defined?(Liquid::C) && Liquid::C.enabled + + template = <<~LIQUID + {% assign x = 1 %} + {{ x }} + {% assign x = 2 %} + {{ x }} + {% assign y = 1 %} + {{ y }} + LIQUID + + parse_context = ParseContext.new(expression_cache: true) + + Liquid::Template.parse(template, parse_context).render + + cache = parse_context.instance_variable_get(:@expression_cache) + + assert_equal( + ["1", "2", "x", "y"], + cache.to_a.map { _1[0] }.sort, + ) + end + + def test_expression_cache_with_lru_redux + skip("Liquid-C does not support Expression caching") if defined?(Liquid::C) && Liquid::C.enabled + cache = LruRedux::Cache.new(10) template = <<~LIQUID {% assign x = 1 %}