Home >Backend Development >PHP Tutorial >Example tutorial on using php static variables as cache
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:
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. |