Created
July 14, 2019 11:40
-
-
Save robertomiranda/8642a6c325e8b763cbf3037b9ab415be to your computer and use it in GitHub Desktop.
This file contains hidden or 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
task :build_top_fans do |_, args| | |
user_id = args[:user_id] | |
redis_client = Redis.new(configurations) | |
top_fan_ids = bookmarker_fan_ids(user_id) + | |
commenters_fan_ids(user_id) + | |
cooker_fan_ids(user_id) | |
top_fan_ids.each do |id| | |
redis_client.sadd("top-fans:#{user_id}", 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.smembers(top_fan_key).sample(10) | |
end | |
def top_fans | |
User.where(id: top_fan_ids) | |
end | |
def is_fan?(other_id) | |
redis_client.sismember(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