Rename `cache_*` methods to `preload_*` in controller concern (#30209)

main
Matt Jankowski 2024-05-16 04:03:46 -04:00 committed by GitHub
parent f0d6dc4519
commit 65e82211cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 56 additions and 54 deletions

View File

@ -25,7 +25,7 @@ class AccountsController < ApplicationController
limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
@statuses = filtered_statuses.without_reblogs.limit(limit) @statuses = filtered_statuses.without_reblogs.limit(limit)
@statuses = cache_collection(@statuses, Status) @statuses = preload_collection(@statuses, Status)
end end
format.json do format.json do

View File

@ -18,7 +18,7 @@ class ActivityPub::CollectionsController < ActivityPub::BaseController
def set_items def set_items
case params[:id] case params[:id]
when 'featured' when 'featured'
@items = for_signed_account { cache_collection(@account.pinned_statuses, Status) } @items = for_signed_account { preload_collection(@account.pinned_statuses, Status) }
@items = @items.map { |item| item.distributable? ? item : ActivityPub::TagManager.instance.uri_for(item) } @items = @items.map { |item| item.distributable? ? item : ActivityPub::TagManager.instance.uri_for(item) }
when 'tags' when 'tags'
@items = for_signed_account { @account.featured_tags } @items = for_signed_account { @account.featured_tags }

View File

@ -60,7 +60,7 @@ class ActivityPub::OutboxesController < ActivityPub::BaseController
def set_statuses def set_statuses
return unless page_requested? return unless page_requested?
@statuses = cache_collection_paginated_by_id( @statuses = preload_collection_paginated_by_id(
AccountStatusesFilter.new(@account, signed_request_account).results, AccountStatusesFilter.new(@account, signed_request_account).results,
Status, Status,
LIMIT, LIMIT,

View File

@ -19,11 +19,11 @@ class Api::V1::Accounts::StatusesController < Api::BaseController
end end
def load_statuses def load_statuses
@account.unavailable? ? [] : cached_account_statuses @account.unavailable? ? [] : preloaded_account_statuses
end end
def cached_account_statuses def preloaded_account_statuses
cache_collection_paginated_by_id( preload_collection_paginated_by_id(
AccountStatusesFilter.new(@account, current_account, params).results, AccountStatusesFilter.new(@account, current_account, params).results,
Status, Status,
limit_param(DEFAULT_STATUSES_LIMIT), limit_param(DEFAULT_STATUSES_LIMIT),

View File

@ -13,11 +13,11 @@ class Api::V1::BookmarksController < Api::BaseController
private private
def load_statuses def load_statuses
cached_bookmarks preloaded_bookmarks
end end
def cached_bookmarks def preloaded_bookmarks
cache_collection(results.map(&:status), Status) preload_collection(results.map(&:status), Status)
end end
def results def results

View File

@ -13,11 +13,11 @@ class Api::V1::FavouritesController < Api::BaseController
private private
def load_statuses def load_statuses
cached_favourites preloaded_favourites
end end
def cached_favourites def preloaded_favourites
cache_collection(results.map(&:status), Status) preload_collection(results.map(&:status), Status)
end end
def results def results

View File

@ -41,7 +41,7 @@ class Api::V1::Notifications::RequestsController < Api::BaseController
) )
NotificationRequest.preload_cache_collection(requests) do |statuses| NotificationRequest.preload_cache_collection(requests) do |statuses|
cache_collection(statuses, Status) preload_collection(statuses, Status)
end end
end end

View File

@ -41,7 +41,7 @@ class Api::V1::NotificationsController < Api::BaseController
) )
Notification.preload_cache_collection_target_statuses(notifications) do |target_statuses| Notification.preload_cache_collection_target_statuses(notifications) do |target_statuses|
cache_collection(target_statuses, Status) preload_collection(target_statuses, Status)
end end
end end

View File

@ -26,13 +26,13 @@ class Api::V1::StatusesController < Api::BaseController
DESCENDANTS_DEPTH_LIMIT = 20 DESCENDANTS_DEPTH_LIMIT = 20
def index def index
@statuses = cache_collection(@statuses, Status) @statuses = preload_collection(@statuses, Status)
render json: @statuses, each_serializer: REST::StatusSerializer render json: @statuses, each_serializer: REST::StatusSerializer
end end
def show def show
cache_if_unauthenticated! cache_if_unauthenticated!
@status = cache_collection([@status], Status).first @status = preload_collection([@status], Status).first
render json: @status, serializer: REST::StatusSerializer render json: @status, serializer: REST::StatusSerializer
end end
@ -51,8 +51,8 @@ class Api::V1::StatusesController < Api::BaseController
ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(ancestors_limit, current_account) ancestors_results = @status.in_reply_to_id.nil? ? [] : @status.ancestors(ancestors_limit, current_account)
descendants_results = @status.descendants(descendants_limit, current_account, descendants_depth_limit) descendants_results = @status.descendants(descendants_limit, current_account, descendants_depth_limit)
loaded_ancestors = cache_collection(ancestors_results, Status) loaded_ancestors = preload_collection(ancestors_results, Status)
loaded_descendants = cache_collection(descendants_results, Status) loaded_descendants = preload_collection(descendants_results, Status)
@context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants) @context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants)
statuses = [@status] + @context.ancestors + @context.descendants statuses = [@status] + @context.ancestors + @context.descendants

View File

@ -21,11 +21,11 @@ class Api::V1::Timelines::HomeController < Api::V1::Timelines::BaseController
private private
def load_statuses def load_statuses
cached_home_statuses preloaded_home_statuses
end end
def cached_home_statuses def preloaded_home_statuses
cache_collection home_statuses, Status preload_collection home_statuses, Status
end end
def home_statuses def home_statuses

View File

@ -21,11 +21,11 @@ class Api::V1::Timelines::ListController < Api::V1::Timelines::BaseController
end end
def set_statuses def set_statuses
@statuses = cached_list_statuses @statuses = preloaded_list_statuses
end end
def cached_list_statuses def preloaded_list_statuses
cache_collection list_statuses, Status preload_collection list_statuses, Status
end end
def list_statuses def list_statuses

View File

@ -18,11 +18,11 @@ class Api::V1::Timelines::PublicController < Api::V1::Timelines::BaseController
end end
def load_statuses def load_statuses
cached_public_statuses_page preloaded_public_statuses_page
end end
def cached_public_statuses_page def preloaded_public_statuses_page
cache_collection(public_statuses, Status) preload_collection(public_statuses, Status)
end end
def public_statuses def public_statuses

View File

@ -23,11 +23,11 @@ class Api::V1::Timelines::TagController < Api::V1::Timelines::BaseController
end end
def load_statuses def load_statuses
cached_tagged_statuses preloaded_tagged_statuses
end end
def cached_tagged_statuses def preloaded_tagged_statuses
@tag.nil? ? [] : cache_collection(tag_timeline_statuses, Status) @tag.nil? ? [] : preload_collection(tag_timeline_statuses, Status)
end end
def tag_timeline_statuses def tag_timeline_statuses

View File

@ -20,7 +20,7 @@ class Api::V1::Trends::StatusesController < Api::BaseController
def set_statuses def set_statuses
@statuses = if enabled? @statuses = if enabled?
cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status) preload_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status)
else else
[] []
end end

View File

@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
include UserTrackingConcern include UserTrackingConcern
include SessionTrackingConcern include SessionTrackingConcern
include CacheConcern include CacheConcern
include PreloadingConcern
include DomainControlHelper include DomainControlHelper
include DatabaseHelper include DatabaseHelper
include AuthorizedFetchHelper include AuthorizedFetchHelper

View File

@ -45,20 +45,4 @@ module CacheConcern
Rails.cache.write(key, response.body, expires_in: expires_in, raw: true) Rails.cache.write(key, response.body, expires_in: expires_in, raw: true)
end end
end end
# TODO: Rename this method, as it does not perform any caching anymore.
def cache_collection(raw, klass)
return raw unless klass.respond_to?(:preload_cacheable_associations)
records = raw.to_a
klass.preload_cacheable_associations(records)
records
end
# TODO: Rename this method, as it does not perform any caching anymore.
def cache_collection_paginated_by_id(raw, klass, limit, options)
cache_collection raw.to_a_paginated_by_id(limit, options), klass
end
end end

View File

@ -0,0 +1,17 @@
# frozen_string_literal: true
module PreloadingConcern
extend ActiveSupport::Concern
def preload_collection(scope, klass)
return scope unless klass.respond_to?(:preload_cacheable_associations)
scope.to_a.tap do |records|
klass.preload_cacheable_associations(records)
end
end
def preload_collection_paginated_by_id(scope, klass, limit, options)
preload_collection scope.to_a_paginated_by_id(limit, options), klass
end
end

View File

@ -45,7 +45,7 @@ class TagsController < ApplicationController
end end
def set_statuses def set_statuses
@statuses = cache_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status) @statuses = preload_collection(TagFeed.new(@tag, nil, local: @local).get(limit_param), Status)
end end
def limit_param def limit_param

View File

@ -2,20 +2,20 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe CacheConcern do RSpec.describe PreloadingConcern do
controller(ApplicationController) do controller(ApplicationController) do
include CacheConcern include PreloadingConcern
def empty_array def empty_array
render plain: cache_collection([], Status).size render plain: preload_collection([], Status).size
end end
def empty_relation def empty_relation
render plain: cache_collection(Status.none, Status).size render plain: preload_collection(Status.none, Status).size
end end
def account_statuses_favourites def account_statuses_favourites
render plain: cache_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count) render plain: preload_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count)
end end
end end
@ -27,7 +27,7 @@ RSpec.describe CacheConcern do
end end
end end
describe '#cache_collection' do describe '#preload_collection' do
context 'when given an empty array' do context 'when given an empty array' do
it 'returns an empty array' do it 'returns an empty array' do
get :empty_array get :empty_array