PHP statistics on the number of online users
- /**
- * Created by PhpStorm.
- * User: jifei
- * Date: 15/11/24
- * Time: 20: 58
- *
- * Million users per minute, real-time statistics of the total number of online users in the last 15 minutes
- */
- Class OnlineUser
- {
- Public $ prefix_key = "online"; // key prefix
- Public function _ construct ()
- {
- $ This-> redis = new Redis ();
- }
- /**
- * Add a new online user to the collection.
- *
- * @ Param $ uid
- */
- Public function addUser ($ uid)
- {
- $ This-> redis-> sAdd ($ this-> prefix_key. date ('hi'), $ uid );
- }
- /**
- * Obtain the number of online users
- *
- * @ Param $ start_min start minute hi format
- * @ Param $ end_min the minute after the end of the statistics
- *
- * @ Return mixed
- */
- Public function userNum ($ start_min, $ end_min)
- {
- // The first parameter, the name of the Union key
- $ Params [] = $ this-> prefix_key. $ start_min. '_'. $ end_min;
- // Traverse all the minutes in the time interval and put them in the parameter
- For ($ min = $ start_min; $ min <$ end_min; $ min ++ ){
- $ Params [] = $ this-> prefix_key. $ min;
- }
- // Calculate and save the union of all users in minutes. The performance is much faster than that returned by direct computing, saving data transmission.
- $ Num = call_user_func_array ([$ this-> redis, "sUnionStore"], $ params );
- // Delete the temporary union
- $ This-> redis-> delete ($ params [0]);
- Return $ num;
- }
- }
|
PHP