Php static variables used as cache instance tutorial

Source: Internet
Author: User
Php static variables used as cache instance tutorial

  1. /**

  2. * The receiver that the user requests to reset the password
  3. */
  4. Function requestResetPassword (){
  5. // Check whether 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 an email to the user
  11. SendEmail ($ _ GET ['userid'], 'password reset successfully', and 'New password is xxx ');
  12. Exit ('the new password has been sent to your mailbox. ');
  13. }

  14. /**

  15. * Helping users reset their passwords
  16. */
  17. Function resetPassword ($ userid ){
  18. // Check whether the user exists
  19. If (! CheckUserExists ($ userid )){
  20. Return false;
  21. }

  22. // Reset the user password

  23. // Omitted...
  24. Return true;
  25. }

  26. /**

  27. * Send an email to the user
  28. */
  29. Function sendEmail ($ userid, $ title, $ content ){
  30. // Check whether the user exists
  31. If (! CheckUserExists ($ userid )){
  32. Return false;
  33. }

  34. // Send email

  35. // Omitted...
  36. Return true;
  37. }

  38. /**

  39. * Check whether a user exists
  40. */
  41. Function checkUserExists ($ userid ){
  42. $ User = getUserInfo ($ userid );
  43. Return! Empty ($ user );
  44. }

  45. /**

  46. * Obtain the data of a user.
  47. */
  48. Function getUserInfo ($ userid ){
  49. // Assume that I have a query function used to query the database and return data.
  50. $ User = query ("SELECT * FROM 'user' WHERE 'uid' =". intval ($ userid ));
  51. Return is_array ($ user )? $ User: array ();
  52. }

Problem: all three functions use the checkUserExists function to check whether the user does not exist. the database queries three times, resulting in some additional overhead. It seems possible to remove any checkUserExists between the three. However, if some functions require calling resetPassword or sendEmail, an error may occur if the user does not exist.

Another solution is to write the resetPassword logic to requestResetPassword. in another step, write the sendEmail logic. In this way, the number of function calls is reduced, and the database query becomes one time, thus improving the performance. However, the password reset and email sending functions cannot be reused and violate the single responsibility principle, and the code complexity is also improved.

However, because function separation and reusability are both good, if the actual performance is affected, you may consider using the cache method to reduce database queries and change the checkUserExists functions they share:

  1. /**

  2. * Check whether a user exists
  3. */
  4. Function checkUserExists ($ userid ){
  5. // Add a cache to record the user's results
  6. Static $ cache = array ();

  7. // Check whether the current user has checked once

  8. If (isset ($ cache [$ userid]) {
  9. Return $ cache [$ userid];
  10. }

  11. $ User = getUserInfo ($ userid );

  12. // Record the result to the cache
  13. $ Cache [$ userid] =! Empty ($ user );

  14. Return $ cache [$ userid];

  15. }

You can also use the same method to change the getUserInfo function.

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

Consider the impact on program performance. for example, when traversing data, traversal may be encapsulated into a function for reuse and used multiple times. These expenses have little impact on the project. Therefore, the focus can be on improving code reusability and maintainability, rather than focusing on wasting more performance. If the actual performance does not meet the requirements, you can also increase the 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.