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.
18 lines
583 B
Ruby
18 lines
583 B
Ruby
class AutocompleteController < ApplicationController
|
|
respond_to :xml, :json
|
|
|
|
def index
|
|
@query = params.dig(:search, :query)
|
|
@type = params.dig(:search, :type)
|
|
@limit = params.fetch(:limit, 10).to_i
|
|
@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
|