Lists redis clean-up (#5886)

* When list is deleted, remove feed from redis

* Clean up list feeds of inactive users
This commit is contained in:
Eugen Rochko 2017-12-05 23:20:27 +01:00 committed by GitHub
parent e20895f251
commit d68868ca14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 10 deletions

View File

@ -19,4 +19,23 @@ class List < ApplicationRecord
has_many :accounts, through: :list_accounts has_many :accounts, through: :list_accounts
validates :title, presence: true validates :title, presence: true
before_destroy :clean_feed_manager
private
def clean_feed_manager
reblog_key = FeedManager.instance.key(:list, id, 'reblogs')
reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)
Redis.current.pipelined do
Redis.current.del(FeedManager.instance.key(:list, id))
Redis.current.del(reblog_key)
reblogged_id_set.each do |reblogged_id|
reblog_set_key = FeedManager.instance.key(:list, id, "reblogs:#{reblogged_id}")
Redis.current.del(reblog_set_key)
end
end
end
end end

View File

@ -5,16 +5,30 @@ class Scheduler::FeedCleanupScheduler
include Sidekiq::Worker include Sidekiq::Worker
def perform def perform
clean_home_feeds!
clean_list_feeds!
end
private
def clean_home_feeds!
clean_feeds!(inactive_account_ids, :home)
end
def clean_list_feeds!
clean_feeds!(inactive_list_ids, :list)
end
def clean_feeds!(ids, type)
reblogged_id_sets = {} reblogged_id_sets = {}
feedmanager = FeedManager.instance
redis.pipelined do redis.pipelined do
inactive_user_ids.each do |account_id| ids.each do |feed_id|
redis.del(feedmanager.key(:home, account_id)) redis.del(feed_manager.key(type, feed_id))
reblog_key = feedmanager.key(:home, account_id, 'reblogs') reblog_key = feed_manager.key(type, feed_id, 'reblogs')
# We collect a future for this: we don't block while getting # We collect a future for this: we don't block while getting
# it, but we can iterate over it later. # it, but we can iterate over it later.
reblogged_id_sets[account_id] = redis.zrange(reblog_key, 0, -1) reblogged_id_sets[feed_id] = redis.zrange(reblog_key, 0, -1)
redis.del(reblog_key) redis.del(reblog_key)
end end
end end
@ -22,19 +36,25 @@ class Scheduler::FeedCleanupScheduler
# Remove all of the reblog tracking keys we just removed the # Remove all of the reblog tracking keys we just removed the
# references to. # references to.
redis.pipelined do redis.pipelined do
reblogged_id_sets.each do |account_id, future| reblogged_id_sets.each do |feed_id, future|
future.value.each do |reblogged_id| future.value.each do |reblogged_id|
reblog_set_key = feedmanager.key(:home, account_id, "reblogs:#{reblogged_id}") reblog_set_key = feed_manager.key(type, feed_id, "reblogs:#{reblogged_id}")
redis.del(reblog_set_key) redis.del(reblog_set_key)
end end
end end
end end
end end
private def inactive_account_ids
@inactive_account_ids ||= User.confirmed.inactive.pluck(:account_id)
end
def inactive_user_ids def inactive_list_ids
@inactive_user_ids ||= User.confirmed.inactive.pluck(:account_id) List.where(account_id: inactive_account_ids).pluck(:id)
end
def feed_manager
FeedManager.instance
end end
def redis def redis