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

27
app/models/feed.rb Normal file
View file

@ -0,0 +1,27 @@
class Feed
def initialize(type, account)
@type = type
@account = account
end
def get(limit, offset = 0)
unhydrated = redis.zrevrange(key, offset, limit)
status_map = Hash.new
# If we're after most recent items and none are there, we need to precompute the feed
return PrecomputeFeedService.new.(@type, @account).take(limit) if unhydrated.empty? && offset == 0
Status.where(id: unhydrated).each { |status| status_map[status.id.to_s] = status }
return unhydrated.map { |id| status_map[id] }
end
private
def key
"feed:#{@type}:#{@account.id}"
end
def redis
$redis
end
end

View file

@ -67,5 +67,6 @@ class Status < ActiveRecord::Base
after_create do
self.account.stream_entries.create!(activity: self)
FanOutOnWriteService.new.(self)
end
end