Last active
December 29, 2015 10:29
-
-
Save huydx/7657519 to your computer and use it in GitHub Desktop.
online_user_count.rb
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
class ApplicationController < ActionController:Base | |
before_action :update_online_status | |
def update_online_status | |
now = DateTime.now | |
expire_time = now + 5.minutes | |
current_time_key = "online_users:#{now.minute}" | |
$redis.sadd(current_time_key, current_user.id) | |
$redis.expireat(current_time_key, expire_time) | |
end | |
def online_users_num | |
now = DateTime.now.minute | |
($redis.sunion (now-5..now).map{|time| "online_users#{time}"}).length | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment