Home > php教程 > PHP源码 > PHP counts online users

PHP counts online users

大家讲道理
Release: 2016-11-09 14:28:51
Original
2032 people have browsed it

<?php
 * 每分钟百万用户,实时统计最近15分钟在线用户总数
 */
class OnlineUser
{
    public $prefix_key = "online";//key前缀
    public function __construct()
    {
        $this->redis = new Redis();
    }
    /**
     * 往集合中添加新的在线用户
     *
     * @param $uid
     */
    public function addUser($uid)
    {
        $this->redis->sAdd($this->prefix_key . date(&#39;hi&#39;), $uid);
    }
    /**
     * 获取在线用户数
     *
     * @param $start_min  统计开始分钟 hi格式
     * @param $end_min    统计结束的分钟
     *
     * @return mixed
     */
    public function userNum($start_min, $end_min)
    {
        //第一个参数,并集的key名称
        $params[] = $this->prefix_key . $start_min . &#39;_&#39; . $end_min;
        //遍历时间区间内所有的分钟,并放入到参数中
        for ($min = $start_min; $min < $end_min; $min++) {
            $params[] = $this->prefix_key . $min;
        }
        //求所有分钟的用户的并集并保存,性能比直接计算返回快很多,省去了数据传输
        $num = call_user_func_array([$this->redis, "sUnionStore"], $params);
        //删除临时并集
        $this->redis->delete($params[0]);
        return $num;
    }
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template