/**
- * 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 ();
- }
Copy CodeQuestion: These three functions all use the Checkuserexists function simultaneously to check that the user does not exist, the database queries 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 ResetPassword into the Requestresetpassword, and a little more, to write the logic of SendEmail. 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 function separation and reusability are good, if actual performance is affected, you might consider using cached methods to reduce database queries and change the checkuserexists functions they share:
/**
- * 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];
- }
Copy CodeYou can also change the GetUserInfo function in the same way. When the reusability of code is improved, it is very simple to improve performance, and the bottleneck of performance is easy to be discovered and modified. Considering the impact on program performance, such as traversing data, you might encapsulate the traversal into a function for reuse and use it multiple times. These costs have a negligible impact on the project at all, not as expected. Therefore, the emphasis can be placed on improving the reusability and maintainability of the code, rather than on the performance of wasting more. If the actual performance is not up to the requirement, you can also weigh the additional hardware configuration. |