From d6a5b9e2527613bd8f4c4677aac2ec9e65e0f3c8 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 13 Dec 2020 00:45:22 -0600 Subject: [PATCH] autocomplete: rework cache policy. The previous cache policy was that all autocomplete results were cached for a fixed 7 days. The new policy is that if autocomplete returns more than 10 results they're cached for 24 hours, otherwise if it returns less than 10 results they're cached for 1 hour. The rationale is that if autocomplete returns a lot of results, then the top 10 results are relatively stable and unlikely to change, but if it returns less than 10 results, then the results are unstable and can be easily changed. We also change it so that autocomplete calls can be cached publicly. Public caching means that HTTP requests are cached by Cloudflare. This will ideally reduce load on the server and reduce latency for end users. This is only safe for calls that return the same results for all users (i.e. the results don't depend on the current user), since the cache is publicly shared by all users. Currently username, favgroup, and saved search autocomplete results depend on the current user, so they can't be publicly cached. --- app/controllers/autocomplete_controller.rb | 4 ++++ app/logical/autocomplete_service.rb | 27 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/controllers/autocomplete_controller.rb b/app/controllers/autocomplete_controller.rb index 68c46d793..0b36168db 100644 --- a/app/controllers/autocomplete_controller.rb +++ b/app/controllers/autocomplete_controller.rb @@ -8,6 +8,10 @@ class AutocompleteController < ApplicationController @autocomplete = AutocompleteService.new(@query, @type, current_user: CurrentUser.user, limit: @limit) @results = @autocomplete.autocomplete_results + @expires_in = @autocomplete.cache_duration + @public = @autocomplete.cache_publicly? + + expires_in @expires_in, public: @public unless response.cache_control.present? respond_with(@results) end end diff --git a/app/logical/autocomplete_service.rb b/app/logical/autocomplete_service.rb index d05cb6696..8ae91392d 100644 --- a/app/logical/autocomplete_service.rb +++ b/app/logical/autocomplete_service.rb @@ -1,4 +1,6 @@ class AutocompleteService + extend Memoist + POST_STATUSES = %w[active deleted pending flagged appealed banned modqueue unmoderated] STATIC_METATAGS = { @@ -197,4 +199,29 @@ class AutocompleteService results = autocomplete_tag(string).map { |result| result[:value] } [query, results] end + + def cache_duration + if autocomplete_results.size == limit + 24.hours + else + 1.hour + end + end + + # Queries that don't depend on the current user are safe to cache publicly. + def cache_publicly? + if type == :tag_query && parsed_search&.type == :tag + true + elsif type.in?(%i[tag artist wiki_page pool opensearch]) + true + else + false + end + end + + def parsed_search + PostQueryBuilder.new(query).terms.first + end + + memoize :autocomplete_results end