Distrubute statuses as a fan-out-on-write system, with optional precomputing
This commit is contained in:
parent
fe57f6330f
commit
6c4c84b161
9 changed files with 119 additions and 3 deletions
35
app/services/precompute_feed_service.rb
Normal file
35
app/services/precompute_feed_service.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
class PrecomputeFeedService < BaseService
|
||||
MAX_FEED_SIZE = 800
|
||||
|
||||
# Fill up a user's home/mentions feed from DB and return it
|
||||
# @param [Symbol] type :home or :mentions
|
||||
# @param [Account] account
|
||||
# @return [Array]
|
||||
def call(type, account)
|
||||
statuses = send(type.to_s, account).order('created_at desc').limit(MAX_FEED_SIZE)
|
||||
statuses.each { |status| push(type, account.id, status) }
|
||||
statuses
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def push(type, receiver_id, status)
|
||||
redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
|
||||
end
|
||||
|
||||
def home(account)
|
||||
Status.where(account: [account] + account.following)
|
||||
end
|
||||
|
||||
def mentions(account)
|
||||
Status.where(id: Mention.where(account: account).pluck(:status_id))
|
||||
end
|
||||
|
||||
def key(type, id)
|
||||
"feed:#{type}:#{id}"
|
||||
end
|
||||
|
||||
def redis
|
||||
$redis
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue