Cache Status#ancestors recursive query results (#2527)

signup-info-prompt
Eugen Rochko 2017-04-27 17:07:15 +02:00 committed by GitHub
parent 2af4f3c4e2
commit d4fedf84e0
1 changed files with 1 additions and 1 deletions

View File

@ -91,7 +91,7 @@ class Status < ApplicationRecord
end end
def ancestors(account = nil) def ancestors(account = nil)
ids = (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) ids = Rails.cache.fetch("ancestors:#{id}") { (Status.find_by_sql(['WITH RECURSIVE search_tree(id, in_reply_to_id, path) AS (SELECT id, in_reply_to_id, ARRAY[id] FROM statuses WHERE id = ? UNION ALL SELECT statuses.id, statuses.in_reply_to_id, path || statuses.id FROM search_tree JOIN statuses ON statuses.id = search_tree.in_reply_to_id WHERE NOT statuses.id = ANY(path)) SELECT id FROM search_tree ORDER BY path DESC', id]) - [self]).pluck(:id) }
statuses = Status.where(id: ids).group_by(&:id) statuses = Status.where(id: ids).group_by(&:id)
results = ids.map { |id| statuses[id].first } results = ids.map { |id| statuses[id].first }
results = results.reject { |status| filter_from_context?(status, account) } results = results.reject { |status| filter_from_context?(status, account) }