Home > Backend Development > PHP Tutorial > PHP counts online users

PHP counts online users

WBOY
Release: 2016-07-25 08:42:17
Original
1073 people have browsed it
  1. /**
  2. * Created by PhpStorm.
  3. * User: jifei
  4. * Date: 15/11/24
  5. * Time: 20:58
  6. *
  7. * Millions of users per minute, real-time statistics of the total number of online users in the last 15 minutes
  8. */
  9. class OnlineUser
  10. {
  11. public $prefix_key = "online";//key prefix
  12. public function __construct()
  13. {
  14. $this->redis = new Redis();
  15. }
  16. /**
  17. * Add new online users to the collection
  18. *
  19. * @param $uid
  20. */
  21. public function addUser($uid)
  22. {
  23. $this->redis->sAdd($this->prefix_key . date('hi '), $uid);
  24. }
  25. /**
  26. * Get the number of online users
  27. *
  28. * @param $start_min Statistics start minute hi format
  29. * @param $end_min Statistics end minutes
  30. *
  31. * @return mixed
  32. */
  33. public function userNum($start_min, $end_min)
  34. {
  35. //The first parameter, the key name of the union
  36. $params[] = $this->prefix_key . $start_min . '_' . $end_min;
  37. //Traverse all the minutes in the time interval and put them into the parameters
  38. for ($min = $start_min; $min < $end_min; $min++) {
  39. $params[] = $this->prefix_key . $min;
  40. }
  41. //Find the union of all minute users and save it. The performance is much faster than direct calculation and return, eliminating the need for data transmission
  42. $num = call_user_func_array([$this->redis, "sUnionStore"], $params);
  43. //Delete the temporary union
  44. $this->redis->delete($params[0]);
  45. return $num;
  46. }
  47. }
Copy code

PHP


Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template