How to use PHP static variables as cache_PHP tutorial

WBOY
Release: 2016-07-13 10:25:12
Original
799 people have browsed it

The following PHP code example functions to help users reset their passwords. requestResetPassword receives the user's request to reset their password and performs corresponding checks. For better reusability, I assigned the password reset operation to a new resetPassword function separately. After changing the password, I called sendEmail to send a notification email to the user.

Copy code The code is as follows:

/**
* Receiver of user request to reset password
*/
function requestResetPassword() {
//Check if the user exists
if( !checkUserExists( $_GET['userid'] ) ) {
exit('Sorry, the user does not exist, please confirm the user account.');
}
resetPassword( $_GET['userid'] );
//Finally send an email to the user
sendEmail( $_GET['userid'], 'Password reset successful', 'The new password is xxxx' );
exit('The new password has been sent to your email.');
}

/**
* Help users reset their passwords
*/
function resetPassword( $userid ) {
//Check if the user exists
if( !checkUserExists( $userid ) ) {
return false;
}

//Reset user password
//Slightly...
return true;
}

/**
* Send an email to the user
*/
function sendEmail( $userid, $title, $content ) {
/ /Check if the user exists
if( !checkUserExists( $userid ) ) {
return false;
}

//Send email operation
//Omitted...
return true;
}

/**
* Check if a user exists
*/
function checkUserExists( $userid ) {
$user = getUserInfo( $userid );
return !empty ( $user );
}

/**
* Get a user’s data
*/
function getUserInfo( $userid ) {
//Suppose I have a query function, which is used to query Database and return data
$user = query( "SELECT * FROM `user` WHERE `uid`=" . intval( $userid ) );
return is_array( $user ) ? $user : array() ;
}

Now the problem is that 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. I changed the checkUserExists function they share:
Copy code The code is as follows:

/**
* Check if a user exists
*/
function checkUserExists( $userid ) {
//Add a cache, Used to record the results of checking the user
static $cache = array();

//Check whether the current user has already checked once
if( isset( $cache[ $userid ] ) ) {
         return $cache[ $userid]; = !empty( $user );

return $cache[ $userid ];
}


You can also use the same method to modify the getUserInfo function.
You can see here that when the reusability of code is improved, it is very simple to improve performance, and performance bottlenecks are also easy to be discovered and modified.
Although this example does not have a big enough impact on performance, there are some that have a greater impact, such as traversal. I may encapsulate the traversal into a function for reuse and use it multiple times. These expenses did not have as big an impact on my project as expected, or they were minimal. So I prefer to spend my time on how to improve the reusability and maintainability of the code, rather than worrying about wasting more performance. If the actual performance really does not meet the requirements, you can also consider increasing the hardware configuration.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825179.htmlTechArticleThe following PHP code example functions to help users reset their passwords. requestResetPassword is to receive the user’s request to reset their password. And made corresponding inspections. For better reusability, I...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!