diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-08 20:16:11 +0100 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-03-08 20:20:45 +0100 |
commit | 6c4c84b161947cb11ad0451a39e26b25be4c93d5 (patch) | |
tree | fa2a6f4aaff71fcf76c745a57cb7732102814871 /app/models | |
parent | fe57f6330f089d023f0fa4db7f7c8a51551d2ee9 (diff) |
Distrubute statuses as a fan-out-on-write system, with optional precomputing
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/feed.rb | 27 | ||||
-rw-r--r-- | app/models/status.rb | 1 |
2 files changed, 28 insertions, 0 deletions
diff --git a/app/models/feed.rb b/app/models/feed.rb new file mode 100644 index 000000000..a063ad05b --- /dev/null +++ b/app/models/feed.rb @@ -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 diff --git a/app/models/status.rb b/app/models/status.rb index a346ac9b0..7e0c334ec 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -67,5 +67,6 @@ class Status < ActiveRecord::Base after_create do self.account.stream_entries.create!(activity: self) + FanOutOnWriteService.new.(self) end end |