Last active
April 7, 2022 21:10
-
-
Save robertomiranda/061d397d05c391c81bc878fdca2c0d1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMMENTS_SCORE = 1 | |
BOOKMARK_SCORE = 2 | |
COOKS_SCORE = 3 | |
task :build_top_fans do |_, args| | |
user_id = args[:user_id] | |
redis_client = Redis.new(configurations) | |
top_fan_ids_with_scores = group_by_id_and_sum_scopres( bookmarker_fan_ids_with_score(user_id) + | |
commenters_fan_ids_with_scope(user_id) + | |
cooker_fan_ids_with_score(user_id) | |
) | |
top_fan_ids_with_scores.each do |id, score| | |
redis_client.zadd("top-fans:#{user_id}", score, id) | |
end | |
end | |
class User | |
def top_fan_ids | |
# top-fans:user_1: [17, 15, 11, 8, 14, 12, 2, 16, 5, 13] | |
redis_client.zrangebyscore(top_fan_key, "+inf", "-inf") | |
end | |
def top_fans | |
User.where(id: top_fan_ids) | |
end | |
def is_fan?(other_id) | |
!!redis_client.zrank(top_fan_key, other_id) | |
end | |
private | |
def top_fan_key | |
"top-fans:#{id}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment