From 6c4c84b161947cb11ad0451a39e26b25be4c93d5 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 8 Mar 2016 20:16:11 +0100 Subject: Distrubute statuses as a fan-out-on-write system, with optional precomputing --- app/models/feed.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/models/feed.rb (limited to 'app/models/feed.rb') 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 -- cgit