Tips for using static variables for caching in PHP

WBOY
Release: 2016-07-25 09:05:32
Original
1034 people have browsed it
  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

Advantages and disadvantages of static variables to implement caching:

Advantages: Fast, efficient and simple to implement. Because it is an internal variable of PHP, it is the most efficient among all caches.

Disadvantages: It has poor flexibility, is only valid in this connection, has a small execution area, is only valid in the same function, and cannot operate across functions (global variables can be used instead).

Summary: Static variables are very easy to use for caching and do not consume many resources. For those that need to query the database and may be executed multiple times in one connection, you might as well add it. Although the effect may be limited.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template