<?php * 每分鐘百萬用戶,實時統(tǒng)計最近15分鐘在線用戶總數(shù) */ 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('hi'), $uid); } /** * 獲取在線用戶數(shù) * * @param $start_min 統(tǒng)計開始分鐘 hi格式 * @param $end_min 統(tǒng)計結(jié)束的分鐘 * * @return mixed */ public function userNum($start_min, $end_min) { //第一個參數(shù),并集的key名稱 $params[] = $this->prefix_key . $start_min . '_' . $end_min; //遍歷時間區(qū)間內(nèi)所有的分鐘,并放入到參數(shù)中 for ($min = $start_min; $min < $end_min; $min++) { $params[] = $this->prefix_key . $min; } //求所有分鐘的用戶的并集并保存,性能比直接計算返回快很多,省去了數(shù)據(jù)傳輸 $num = call_user_func_array([$this->redis, "sUnionStore"], $params); //刪除臨時并集 $this->redis->delete($params[0]); return $num; } }
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

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
How to fix KB5060533 fails to install in Windows 10?
3 weeks ago
By DDD
Dune: Awakening - Where To Get Insulated Fabric
3 weeks ago
By Jack chen
Gmail Login: How to Sign Up, Sign In, or Sign Out of Gmail - MiniTool
1 months ago
By Jack chen
How to fix KB5060999 fails to install in Windows 11?
3 weeks ago
By DDD
Guild Guide In Tainted Grail: The Fall Of Avalon
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
