diff --git a/app/logical/related_tag_calculator.rb b/app/logical/related_tag_calculator.rb index 9a999f5a3..8baf2fd93 100644 --- a/app/logical/related_tag_calculator.rb +++ b/app/logical/related_tag_calculator.rb @@ -29,4 +29,14 @@ module RelatedTagCalculator tags_with_counts = posts.flat_map(&:tag_array).group_by(&:itself).transform_values(&:size) tags_with_counts.sort_by { |tag_name, count| [-count, tag_name] }.map(&:first) end + + def self.cached_similar_tags_for_search(tag_query, max_tags, search_timeout: 2000, cache_timeout: 8.hours) + Cache.get("similar_tags:#{tag_query}", cache_timeout, race_condition_ttl: 60.seconds) do + ApplicationRecord.with_timeout(search_timeout, []) do + CurrentUser.without_safe_mode do + RelatedTagCalculator.similar_tags_for_search(tag_query).take(max_tags).pluck(:name) + end + end + end + end end diff --git a/app/presenters/post_set_presenters/post.rb b/app/presenters/post_set_presenters/post.rb index a57b60bc0..082cf95e4 100644 --- a/app/presenters/post_set_presenters/post.rb +++ b/app/presenters/post_set_presenters/post.rb @@ -40,11 +40,7 @@ module PostSetPresenters end def similar_tags - Cache.get("similar_tags:#{post_set.tag_string}", 4.hours, race_condition_ttl: 60.seconds) do - ApplicationRecord.with_timeout(1_000, []) do - RelatedTagCalculator.similar_tags_for_search(post_set.tag_string).take(MAX_TAGS).pluck(:name) - end - end + RelatedTagCalculator.cached_similar_tags_for_search(post_set.tag_string, MAX_TAGS) end def frequent_tags