Created
April 5, 2013 22:25
-
-
Save derwiki/5323177 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
| module RedisFriends | |
| def self.key user_id | |
| "redis_friends:#{ user_id }" | |
| end | |
| def self.sync_friend_ids user_id, friend_ids | |
| REDIS.multi do | |
| REDIS.sadd key(user_id), friend_ids | |
| friend_ids.each { |friend_id| REDIS.sadd key(friend_id), user_id } | |
| end | |
| end | |
| def self.friend_ids user_id | |
| REDIS.smembers(key(user_id)).map(&:to_i) | |
| end | |
| def self.mutual_friends a, b | |
| REDIS.sinter(key(a), key(b)).map(&:to_i) | |
| end | |
| def self.multiple_mutual_friends user_id, friend_ids | |
| friend_ids.inject({}) do |acc, friend_id| | |
| acc.merge friend_id => mutual_friends(user_id, friend_id) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment