]> cat aescling's git repositories - mastodon.git/commitdiff
Reduce number of commands in FeedManager#trim (#3989)
authorAkihiko Odaki (@fn_aki@pawoo.net) <akihiko.odaki.4i@stu.hosei.ac.jp>
Wed, 28 Jun 2017 23:17:26 +0000 (08:17 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Wed, 28 Jun 2017 23:17:26 +0000 (01:17 +0200)
app/lib/feed_manager.rb
spec/lib/feed_manager_spec.rb

index 90a1441f293089740d06e773429c468319e4f9dd..c507f2636535244de565f373df1fbfa8ead4b692 100644 (file)
@@ -38,9 +38,7 @@ class FeedManager
   end
 
   def trim(type, account_id)
-    return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
-    last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
-    redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
+    redis.zremrangebyrank(key(type, account_id), '0', (-(FeedManager::MAX_ITEMS + 1)).to_s)
   end
 
   def push_update_required?(timeline_type, account_id)
index bf474c35430622f12913f7e9c9b4306b42c8560c..4bdc96866d1ba6710d73a7f6a2e44da0baadc11a 100644 (file)
@@ -131,4 +131,17 @@ RSpec.describe FeedManager do
       end
     end
   end
+
+  describe '#push' do
+    it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
+      account = Fabricate(:account)
+      status = Fabricate(:status)
+      members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] }
+      Redis.current.zadd("feed:type:#{account.id}", members)
+
+      FeedManager.instance.push('type', account, status)
+
+      expect(Redis.current.zcard("feed:type:#{account.id}")).to eq FeedManager::MAX_ITEMS
+    end
+  end
 end