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
27
app/models/feed.rb
Normal file
27
app/models/feed.rb
Normal 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
|
|
@ -67,5 +67,6 @@ class Status < ActiveRecord::Base
|
|||
|
||||
after_create do
|
||||
self.account.stream_entries.create!(activity: self)
|
||||
FanOutOnWriteService.new.(self)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue