0
0
Fork 0

Distrubute statuses as a fan-out-on-write system, with optional precomputing

This commit is contained in:
Eugen Rochko 2016-03-08 20:16:11 +01:00
parent fe57f6330f
commit 6c4c84b161
9 changed files with 119 additions and 3 deletions

View 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