PHP static variable as an instance of caching tutorial

Source: Internet
Author: User
  1. /**

  2. * User request to reset password receiver
  3. */
  4. function Requestresetpassword () {
  5. Check if the user exists
  6. if (!checkuserexists ($_get[' userid ')) {
  7. Exit (' Sorry, the user does not exist, please confirm the user account. ');
  8. }
  9. ResetPassword ($_get[' userid ');
  10. Finally send a message to the user
  11. SendEmail ($_get[' userid '), ' Reset password succeeded ', ' new password is xxxx ');
  12. Exit (' The new password has been sent to your mailbox. ');
  13. }

  14. /**

  15. * Help users reset their passwords
  16. */
  17. function ResetPassword ($userid) {
  18. Check if the user exists
  19. if (!checkuserexists ($userid)) {
  20. return false;
  21. }

  22. To reset the user's password

  23. Slightly...
  24. return true;
  25. }

  26. /**

  27. * Send an email to the user
  28. */
  29. function SendEmail ($userid, $title, $content) {
  30. Check if the user exists
  31. if (!checkuserexists ($userid)) {
  32. return false;
  33. }

  34. Send mail action

  35. Slightly...
  36. return true;
  37. }

  38. /**

  39. * Check if a user exists
  40. */
  41. function Checkuserexists ($userid) {
  42. $user = GetUserInfo ($userid);
  43. Return!empty ($user);
  44. }

  45. /**

  46. * Get data from a user
  47. */
  48. function GetUserInfo ($userid) {
  49. Suppose I have a query function that is used to query the database and return the data
  50. $user = Query ("SELECT * from ' user ' WHERE ' uid ' =". Intval ($userid));
  51. Return Is_array ($user)? $user: Array ();
  52. }

Copy Code

Question: These three functions all use the Checkuserexists function simultaneously to check that the user does not exist, the database queries three times, which brings some extra overhead. If you want to remove any one of the checkuserexists between the three, it seems possible. However, if there are certain features to call ResetPassword or SendEmail after the user does not exist, the system may get an error.

Another solution is to write the logic of ResetPassword into the Requestresetpassword, and a little more, to write the logic of SendEmail. This reduces the function call, and the database query becomes one time, and the performance is improved. But the ability to reset passwords and send messages will not be reused, and it violates the principle of single responsibility and increases the complexity of the code.

However, because function separation and reusability are good, if actual performance is affected, you might consider using cached methods to reduce database queries and change the checkuserexists functions they share:

    1. /**

    2. * Check if a user exists
    3. */
    4. function Checkuserexists ($userid) {
    5. Add a cache to record the results of checking users
    6. Static $cache = Array ();

    7. Check if the current user has checked once

    8. if (Isset ($cache [$userid])) {
    9. return $cache [$userid];
    10. }

    11. $user = GetUserInfo ($userid);

    12. Record the results in the cache
    13. $cache [$userid] =!empty ($user);

    14. return $cache [$userid];

    15. }

Copy Code

You can also change the GetUserInfo function in the same way.

When the reusability of code is improved, it is very simple to improve performance, and the bottleneck of performance is easy to be discovered and modified.

Considering the impact on program performance, such as traversing data, you might encapsulate the traversal into a function for reuse and use it multiple times. These costs have a negligible impact on the project at all, not as expected. Therefore, the emphasis can be placed on improving the reusability and maintainability of the code, rather than on the performance of wasting more. If the actual performance is not up to the requirement, you can also weigh the additional hardware configuration.

  • 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.