From 053b522a827be3d91eb8dd64f634c09edf1bd28b Mon Sep 17 00:00:00 2001 From: ashmaroli Date: Thu, 12 Apr 2018 21:06:53 +0530 Subject: [PATCH] implement a cache within the where filter (#6868) Merge pull request 6868 --- lib/jekyll/filters.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/jekyll/filters.rb b/lib/jekyll/filters.rb index 067648b7..209a3e0e 100644 --- a/lib/jekyll/filters.rb +++ b/lib/jekyll/filters.rb @@ -167,11 +167,23 @@ module Jekyll # # Returns the filtered array of objects def where(input, property, value) + return input if property.nil? || value.nil? return input unless input.respond_to?(:select) - input = input.values if input.is_a?(Hash) - input.select do |object| - Array(item_property(object, property)).map!(&:to_s).include?(value.to_s) - end || [] + input = input.values if input.is_a?(Hash) + input_id = input.hash + + # implement a hash based on method parameters to cache the end-result + # for given parameters. + @where_filter_cache ||= {} + @where_filter_cache[input_id] ||= {} + @where_filter_cache[input_id][property] ||= {} + + # stash or retrive results to return + @where_filter_cache[input_id][property][value] ||= begin + input.select do |object| + Array(item_property(object, property)).map!(&:to_s).include?(value.to_s) + end || [] + end end # Filters an array of objects against an expression