PHP statistics on the number of online users

Source: Internet
Author: User
PHP statistics on the number of online users

  1. /**
  2. * Created by PhpStorm.
  3. * User: jifei
  4. * Date: 15/11/24
  5. * Time: 20: 58
  6. *
  7. * Million 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 a new online user 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. * Obtain the number of online users
  27. *
  28. * @ Param $ start_min start minute hi format
  29. * @ Param $ end_min the minute after the end of the statistics
  30. *
  31. * @ Return mixed
  32. */
  33. Public function userNum ($ start_min, $ end_min)
  34. {
  35. // The first parameter, the name of the Union key
  36. $ Params [] = $ this-> prefix_key. $ start_min. '_'. $ end_min;
  37. // Traverse all the minutes in the time interval and put them in the parameter
  38. For ($ min = $ start_min; $ min <$ end_min; $ min ++ ){
  39. $ Params [] = $ this-> prefix_key. $ min;
  40. }
  41. // Calculate and save the union of all users in minutes. The performance is much faster than that returned by direct computing, saving 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. }


PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.