Skip to content

Instantly share code, notes, and snippets.

@huydx
Last active December 29, 2015 10:29
Show Gist options
  • Save huydx/7657519 to your computer and use it in GitHub Desktop.
Save huydx/7657519 to your computer and use it in GitHub Desktop.
online_user_count.rb
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