mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 00:55:11 -07:00
users can provide optional expression cache
This commit is contained in:
@@ -8,6 +8,7 @@ end
|
|||||||
gemspec
|
gemspec
|
||||||
|
|
||||||
gem "base64"
|
gem "base64"
|
||||||
|
gem "lru_redux"
|
||||||
|
|
||||||
group :benchmark, :test do
|
group :benchmark, :test do
|
||||||
gem 'benchmark-ips'
|
gem 'benchmark-ips'
|
||||||
@@ -24,5 +25,3 @@ group :test do
|
|||||||
gem 'rubocop-shopify', '~> 2.12.0', require: false
|
gem 'rubocop-shopify', '~> 2.12.0', require: false
|
||||||
gem 'rubocop-performance', require: false
|
gem 'rubocop-performance', require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
gem "lru_redux"
|
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ module Liquid
|
|||||||
|
|
||||||
# Cache only exists during parsing
|
# Cache only exists during parsing
|
||||||
if cache
|
if cache
|
||||||
cache.fetch(markup) { inner_parse(markup, ss, cache).freeze }
|
return cache[markup] if cache.key?(markup)
|
||||||
|
|
||||||
|
cache[markup] = inner_parse(markup, ss, cache).freeze
|
||||||
else
|
else
|
||||||
inner_parse(markup, ss, nil).freeze
|
inner_parse(markup, ss, nil).freeze
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ module Liquid
|
|||||||
# constructing new StringScanner in Lexer, Tokenizer, etc is expensive
|
# constructing new StringScanner in Lexer, Tokenizer, etc is expensive
|
||||||
# This StringScanner will be shared by all of them
|
# This StringScanner will be shared by all of them
|
||||||
@string_scanner = StringScanner.new("")
|
@string_scanner = StringScanner.new("")
|
||||||
@expression_cache = LruRedux::Cache.new(10_000)
|
|
||||||
|
@expression_cache = if options[:expression_cache] != false
|
||||||
|
options[:expression_cache] || LruRedux::Cache.new(10_000)
|
||||||
|
end
|
||||||
|
|
||||||
self.depth = 0
|
self.depth = 0
|
||||||
self.partial = false
|
self.partial = false
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
require 'lru_redux'
|
||||||
|
|
||||||
class ExpressionTest < Minitest::Test
|
class ExpressionTest < Minitest::Test
|
||||||
def test_keyword_literals
|
def test_keyword_literals
|
||||||
@@ -54,6 +55,44 @@ class ExpressionTest < Minitest::Test
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_expression_cache
|
||||||
|
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 %}
|
||||||
|
{{ 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_disable_expression_cache
|
||||||
|
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 = Liquid::ParseContext.new(expression_cache: false)
|
||||||
|
Liquid::Template.parse(template, parse_context).render
|
||||||
|
assert(parse_context.instance_variable_get(:@expression_cache).nil?)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def assert_expression_result(expect, markup, **assigns)
|
def assert_expression_result(expect, markup, **assigns)
|
||||||
|
|||||||
Reference in New Issue
Block a user