Skip to content

Instantly share code, notes, and snippets.

@larryzhao larryzhao/redis_test.rb
Last active Nov 10, 2016

Embed
What would you like to do?
subscriber_ids = Collection.find(4).likes.pluck(:user_id)
subscription = Subscription.find_by(source_identity: "4:collection")
id = subscription.id
def func1(subscriber_ids, id)
Sider.subscription.with do |conn|
subscriber_ids.each_slice(2000) do |batch|
conn.pipelined do
batch.each do |subscriber_id|
conn.set("users:#{subscriber_id}:total_unread_subscriptions_count", 1)
conn.hincrby("users:#{subscriber_id}:subscription_unread_count", id, 1)
end
end
end
end
end
def func2(subscriber_ids, id)
Sider.subscription.with do |conn|
subscriber_ids.each_slice(2000) do |batch|
conn.pipelined do
key_values = batch.flat_map { |subscriber_id| ["users:#{subscriber_id}:total_unread_subscriptions_count", 1] }
conn.mset(key_values)
batch.each do |subscriber_id|
conn.hincrby("users:#{subscriber_id}:subscription_unread_count", id, 1)
end
end
end
end
end
def func3(subscriber_ids, id)
Sider.subscription.with do |conn|
conn.pipelined do
key_values = subscriber_ids.flat_map { |subscriber_id| ["users:#{subscriber_id}:total_unread_subscriptions_count", 1] }
conn.mset(key_values)
subscriber_ids.each do |subscriber_id|
conn.hincrby("users:#{subscriber_id}:subscription_unread_count", id, 1)
end
end
end
end
Benchmark.bm do |x|
x.report { func1(subscriber_ids, id) }
x.report { func2(subscriber_ids, id) }
x.report { func3(subscriber_ids, id) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.