How to use PHP static variables as cache _ PHP Tutorial

Source: Internet
Author: User
Use PHP static variables as cache methods. The following PHP code instance helps you reset the password. requestResetPassword is used to receive and check the password reset request. For better reusability, the following PHP code instance is used to help users reset their passwords. requestResetPassword is used to receive user password reset requests and check accordingly. For better reusability, I will separately allocate the password reset operation to a new resetPassword function. after changing the password, I will call sendEmail to send a notification email to the user.

The code is as follows:


/**
* The receiver that the user requests to reset the password
*/
Function requestResetPassword (){
// Check whether 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 successfully', and 'New password is xxx ');
Exit ('the new password has been sent to your mailbox. ');
}

/**
* Helping users reset their passwords
*/
Function resetPassword ($ userid ){
// Check whether the user exists
If (! CheckUserExists ($ userid )){
Return false;
}

// Reset the user password
// Omitted...
Return true;
}

/**
* Send an email to the user
*/
Function sendEmail ($ userid, $ title, $ content ){
// Check whether the user exists
If (! CheckUserExists ($ userid )){
Return false;
}

// Send email
// Omitted...
Return true;
}

/**
* Check whether a user exists
*/
Function checkUserExists ($ userid ){
$ User = getUserInfo ($ userid );
Return! Empty ($ user );
}

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


The problem is that the three functions both use the checkUserExists function to check whether the user does not exist and 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, and then 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. I modified the checkUserExists functions they share:

The code is as follows:


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

// Check whether the current user has checked once
If (isset ($ cache [$ userid]) {
Return $ cache [$ userid];
}

$ User = getUserInfo ($ userid );
// Record the result to the cache
$ Cache [$ userid] =! Empty ($ user );

Return $ cache [$ userid];
}


You can also use the same method to change the getUserInfo function.
It can be seen that when code reusability is improved, it is very easy to improve the performance, and the performance bottleneck is easily discovered and modified.
Although this example does not have a big impact on the performance, there are still some greater impacts. for example, traversal may encapsulate traversal into a function for reuse and use it multiple times. These expenses do not have much impact on my project as expected, or are minimal. Therefore, I prefer to spend more time on improving code reusability and maintainability, rather than worrying about wasting more time on this performance. If the actual performance does not meet the requirements, you can also increase the hardware configuration.

Bytes. For better reusability, I...

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.