Cache accounts/:id/statuses and single statuses too
This commit is contained in:
parent
2112a81e86
commit
79a0135869
|
@ -57,7 +57,8 @@ class Api::V1::AccountsController < ApiController
|
|||
end
|
||||
|
||||
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_counters_maps(@statuses)
|
||||
|
@ -120,4 +121,23 @@ class Api::V1::AccountsController < ApiController
|
|||
@followed_by = Account.followed_by_map([@account.id], current_user.account_id)
|
||||
@blocking = Account.blocking_map([@account.id], current_user.account_id)
|
||||
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
|
||||
|
|
|
@ -9,6 +9,8 @@ class Api::V1::StatusesController < ApiController
|
|||
respond_to :json
|
||||
|
||||
def show
|
||||
cached = Rails.cache.read(@status.cache_key)
|
||||
@status = cached unless cached.nil?
|
||||
end
|
||||
|
||||
def context
|
||||
|
|
Loading…
Reference in New Issue