Created
November 8, 2013 16:08
-
-
Save biruwon/7373326 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
# Defining the keys | |
def current_key | |
key(Time.now.strftime("%M")) | |
end | |
def keys_in_last_5_minutes | |
now = Time.now | |
times = (0..5).collect {|n| now - n.minutes } | |
times.collect{ |t| key(t.strftime("%M")) } | |
end | |
def key(minute) | |
"online_users_minute_#{minute}" | |
end | |
# Tracking an Active User | |
def track_user_id(id) | |
key = current_key | |
redis.sadd(key, id) | |
end | |
# Who's online | |
def online_user_ids | |
redis.sunion(*keys_in_last_5_minutes) | |
end | |
def online_friend_ids(interested_user_id) | |
redis.sunionstore("online_users", *keys_in_last_5_minutes) | |
redis.sinter("online_users", "user:#{interested_user_id}:friend_ids") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment