Use static variables as cache

Source: Internet
Author: User

I'm guessing you've all met the following conditions.
I have two tables, magazine (magazine information) and Subscibe (subscription information), in the Subscibe table I have a magazine_id to be associated with the number in the magazine table
Now I'm going to do a list of browsing subscriptions, which contains a list of the names of the magazines (the Name field of the magazine table), and one way is to use the join to make an association between the two tables, replacing the magazine_id with the name, But this list is a search result, the SQL query statement is a concatenation, if plus join, the concatenation of SQL logic will become a bit more complicated, so it is easy to get a select * from Subscibe where ..., and then put the result set Magazine_ ID to the query will be more simple, and there is another problem is that I do not want to be in my program too many repeated queries, such as the result set of 10 data is about the same magazine_id, I will have more than 9 duplicate query, so I decided to do so

function Magazineinfobyid ($magazine _id) {
Global $db;
Static $magazine _info;

if (!isset ($magazine _info[$magazine _id])) {
$magazine _info[$magazine _id] = $db->getrow (' select * FROM magazine WHERE id = '. $magazine _id);
}

return $magazine _info[$magazine _id];
}

while (...) {
$magazine _info = Magazineinfobyid ($magazine _id);
.....
}


Magazineinfobyid () The static variable $magazine_info becomes a cached form of things, in some cases, can greatly reduce the number of queries, avoid duplicate queries

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.