Home  >  Article  >  Backend Development  >  Example tutorial on using php static variables as cache

Example tutorial on using php static variables as cache

WBOY
WBOYOriginal
2016-07-25 08:52:41800browse
  1. /**

  2. * Receiver of user request to reset password
  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 an email to the user
  11. sendEmail( $_GET['userid'], 'Password reset successful', 'The new password is xxxx' );
  12. exit('The new password has been sent to your email.');
  13. }

  14. /**

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

  22. //Reset the user Password operation

  23. //Short of...
  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 email operation

  35. //Omitted...
  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 a user’s data
  47. */
  48. function getUserInfo( $userid ) {
  49. //Suppose I have a query function, which is 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. }

Copy code

Problem: These three functions all use the checkUserExists function at the same time to check that the user does not exist. The database is queried three times, which brings some additional overhead. If you want to remove any checkUserExists among the three, it seems possible. However, if some functions later call resetPassword or sendEmail, and the user does not exist, an error may occur in the system.

Another solution is to write the logic of resetPassword into requestResetPassword, and a little later, write the logic of sendEmail into it as well. In this way, function calls are reduced, database queries become one time, and performance is improved. However, the functions of resetting passwords and sending emails will not be reusable, and violate the principle of single responsibility, and the code complexity will also increase.

However, because function separation and reusability are very good, if the actual performance is affected, you may consider using caching to reduce database queries and modify their shared checkUserExists function:

  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 whether the current user has checked once

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

  11. $user = getUserInfo( $userid );

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

  14. return $ cache[ $userid ];

  15. }

Copy the code

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

When the reusability of code increases, it is very simple to improve performance, and performance bottlenecks are also easy to find and modify.

Consider the impact on program performance. For example, when traversing data, you may encapsulate the traversal into a function for reuse and use it multiple times. These expenses did not have as big an impact on the project as expected, and were minimal. Therefore, the focus can be on improving code reusability and maintainability, rather than dwelling on wasting a little more performance. If the actual performance really does not meet the requirements, you can also consider increasing the hardware configuration.



Statement:
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