PHP tips for caching using static variables

Source: Internet
Author: User
  1. function Updatefirstendfrombatchtasks (...) {
  2. Static $cache =array ();
  3. $val =$ $cache [$key];
  4. if (Is_null ($val)) {
  5. $val =.....//Get the value of $val
  6. $cache [$key]= $val;
  7. }
  8. }
Copy Code

the advantages and disadvantages of a static variable for caching :

Advantages: fast speed, high efficiency, simple implementation. Because it is a PHP internal variable, it is the most efficient execution in all caches.

Disadvantage: Poor flexibility, only valid in this connection, the execution area is small, only valid in the same function, cannot work across functions (can use global variable substitution).

Summary: The static variable cache is very useful, and the cost of a few resources, for the database to query, and in a single connection may be executed multiple times, it may be added. Although the effect may be limited.

  • 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.