Cache accounts/:id/statuses and single statuses too

signup-info-prompt
Eugen Rochko 2016-11-23 18:56:30 +01:00
parent 2112a81e86
commit 79a0135869
2 changed files with 23 additions and 1 deletions

View File

@ -57,7 +57,8 @@ class Api::V1::AccountsController < ApiController
end end
def statuses def statuses
@statuses = @account.statuses.with_includes.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a @statuses = @account.statuses.paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id]).to_a
@statuses = cache(@statuses)
set_maps(@statuses) set_maps(@statuses)
set_counters_maps(@statuses) set_counters_maps(@statuses)
@ -120,4 +121,23 @@ class Api::V1::AccountsController < ApiController
@followed_by = Account.followed_by_map([@account.id], current_user.account_id) @followed_by = Account.followed_by_map([@account.id], current_user.account_id)
@blocking = Account.blocking_map([@account.id], current_user.account_id) @blocking = Account.blocking_map([@account.id], current_user.account_id)
end end
def cache(raw)
uncached_ids = []
cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
raw.each do |status|
uncached_ids << status.id unless cached_keys_with_value.key?(status.cache_key)
end
unless uncached_ids.empty?
uncached = Status.where(id: uncached_ids).with_includes.map { |s| [s.id, s] }.to_h
uncached.values.each do |status|
Rails.cache.write(status.cache_key, status)
end
end
raw.map { |status| cached_keys_with_value[status.cache_key] || uncached[status.id] }
end
end end

View File

@ -9,6 +9,8 @@ class Api::V1::StatusesController < ApiController
respond_to :json respond_to :json
def show def show
cached = Rails.cache.read(@status.cache_key)
@status = cached unless cached.nil?
end end
def context def context