about summary refs log tree commit diff
path: root/app/models/feed.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-03-08 20:16:11 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-03-08 20:20:45 +0100
commit6c4c84b161947cb11ad0451a39e26b25be4c93d5 (patch)
treefa2a6f4aaff71fcf76c745a57cb7732102814871 /app/models/feed.rb
parentfe57f6330f089d023f0fa4db7f7c8a51551d2ee9 (diff)
Distrubute statuses as a fan-out-on-write system, with optional precomputing
Diffstat (limited to 'app/models/feed.rb')
-rw-r--r--app/models/feed.rb27
1 files changed, 27 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