Using PHP static variables when caching the method _php tutorial

Source: Internet
Author: User
The following PHP code example, the function is to help the user to reset the password, Requestresetpassword is to receive the user to reset the password request and do a corresponding check. For better reusability, I assign the reset password operation separately to a new ResetPassword function, and then call SendEmail to send a notification message to the user after the password is changed.
Copy CodeThe code is as follows:
/**
* User request to reset password receiver
*/
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 a message to the user
SendEmail ($_get[' userid '), ' Reset password succeeded ', ' new password is xxxx ');
Exit (' The new password has been sent to your mailbox. ');
}

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

To reset the user's 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 mail action
Slightly...
return true;
}

/**
* Check if a user exists
*/
function Checkuserexists ($userid) {
$user = GetUserInfo ($userid);
Return!empty ($user);
}

/**
* Get data from a user
*/
function GetUserInfo ($userid) {
Suppose I have a query function that is used to query the database and return the data
$user = Query ("SELECT * from ' user ' WHERE ' uid ' =". Intval ($userid));
Return Is_array ($user)? $user: Array ();
}

Now the problem is that all three of these functions use the Checkuserexists function to check that the user does not exist and that the database is queried three times, which brings some extra overhead.
If you want to remove any one of the checkuserexists between the three, it seems possible. However, if there are certain features to call ResetPassword or SendEmail after the user does not exist, the system may get an error.
Another solution is to write the logic of the ResetPassword into the Requestresetpassword, and then a little more, and write the sendemail logic in it. This reduces the function call, and the database query becomes one time, and the performance is improved. But the ability to reset passwords and send messages will not be reused, and it violates the principle of single responsibility and increases the complexity of the code.
However, because the function separation and reusability are good, if the actual performance is affected, may consider the cache method to reduce the database query, I changed their common checkuserexists function:
Copy CodeThe code is as follows:
/**
* Check if a user exists
*/
function Checkuserexists ($userid) {
Add a cache to record the results of checking users
Static $cache = Array ();

Check if the current user has checked once
if (Isset ($cache [$userid])) {
return $cache [$userid];
}

$user = GetUserInfo ($userid);
Record the results in the cache
$cache [$userid] =!empty ($user);

return $cache [$userid];
}

You can also change the GetUserInfo function in the same way.
As you can see here, when the reusability of code increases, it is very easy to improve performance, and the bottleneck of performance is easy to be discovered and modified.
Although the performance impact of this example is not large enough, there are some more impact, such as traversal, I may be used for reuse to wrap the traversal into a function, and use it more than once. These costs have had too much impact, or minimal, on my project at all. So I prefer to spend more time on how to improve the reusability and maintainability of the code, rather than the waste of this performance. If the actual performance is not up to the requirement, you can also weigh the additional hardware configuration.

http://www.bkjia.com/PHPjc/825179.html www.bkjia.com true http://www.bkjia.com/PHPjc/825179.html techarticle The following PHP code example, the function is to help the user to reset the password, Requestresetpassword is to receive the user to reset the password request and do a corresponding check. 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.