A nice introduction to PHP cache classes and PHP caching functions and using _php tutorials

Source: Internet
Author: User
The cache is widely used in practice, which can reduce the access to the server database and improve the running speed. At present, many CMS content management systems frequently use caching mechanism to improve the efficiency of system operation. Here is a well-written cache class, you can refer to the caching mechanism and syntax.

The cache.php code is as follows:

 Cachefilevar and file name $this->cachefile//Dynamic page parameters of different cache files are different, but each dynamic page of all cache files have the same file name, but the extension is different $s =arra          Y (".", "/"), $r =array ("_", ""); $this->cachefilevar=str_replace ($s, $r, $_server["Script_name"]). "          _ ". $_get[_actionvar_]; $this->cachefile= $this->cachefilevar. ".".  MD5 ($_server["Request_uri"]);          }//delete the current page/module cache function Delete () {//delete the current page's cache $d = Dir (_cachepath_);          $strlen =strlen ($this->cachefilevar); Returns all cache filegroups for the current page while (false!== ($entry = $d->read ())) {if (substr ($entry, 0, $strlen) = = $this->c Achefilevar) {if (!unlink (_cachepath_.           /". $entry)" {echo "cache directory cannot be written to"; exit;} }}}//To determine if the cache has been cached, and whether a cache function check () is required () {//If the buffer update interval is set _recachetime_ if (_recache time_+0>0) {//returns the last update time of the current page cache $var = @file (_cachepath_. ")           /". $this->cachefilevar); $var = $var [0]; If the update time exceeds the update interval, delete the cache file if (tIME ()-$var >_recachetime_) {$this->delete (); $ischage =true; }}//returns the cache $file =_cachepath_ for the current page. "          /". $this->cachefile;  Determines if the current page cache exists and the cache function turns on return (File_exists ($file) and _cacheenable_ and! $ischange); }//Reads cache function read () {//returns the cache $file =_cachepath_ for the current page. "          /". $this->cachefile;          Read the contents of the cache file if (_cacheenable_) return ReadFile ($file);  else return false; }//Generate Cache function write ($output) {//returns the cache $file =_cachepath_ for the current page. "          /". $this->cachefile;             If the cache function turns on if (_cacheenable_) {//writes the contents of the output to the cache file $fp = @fopen ($file, ' w ');             if (! @fputs ($FP, $output)) {echo "Template cache write failed"; exit;}             @fclose ($FP); If the cache update interval is set _recachetime_ if (_recachetime_+0>0) {///updates the last update time for the current page cache $fi Le=_cachepath_. "            /". $this->cachefilevar;     $fp = @fopen ($file, ' w ');               if (! @fwrite ($FP, Time ())) {echo "The cache directory cannot be written to"; exit;}            @fclose ($FP); }}}}?>


Use of the class:

 
  Check ()) {  $template = $cache->read ();  } else {   ob_start ();       Ob_implicit_flush (0);  ? >  page content ....  
 
   Write ($template);  }  ? >  


Introduction to the cache correlation function of PHP

Some information, such as constant, but still can be changed in the cache to speed up the display speed, which is very valuable, so-called cache, popular understanding is some of the information stored on the server side of the common. It is in the server with life and death, we can save the cache when the next update time to specify the judgment, for example, to update in 5 minutes, you can record the last update time, and the current time comparison, if more than 5 minutes, read the database, update replace, otherwise directly read the cached data, of course, The cache needs to be activated by the client user only once.

Ob_start () function

Ob_start () function: Opens the output buffer.

function format void Ob_start (void)

Note: When the buffer is active, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.

Flush () function

function format: Flush ()

Description: This function is often used and is highly efficient.

String ob_get_contents () function

Ob_get_contents: Returns the contents of the internal buffer.

function Format: string ob_get_contents (void)

Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.

Ob_get_length () function

Ob_get_length: Returns the length of the internal buffer.

function format: int ob_get_length (void)

Note: This function returns the length of the current buffer, and, like ob_get_contents, returns FALSE if the output buffer is not active.

Ob_end_clean () function

Ob_end_clean: Deletes the contents of the internal buffer and closes the internal buffer.

function format: void Ob_end_clean (void)

Description: This function does not output the contents of the internal buffer but deletes it.

Ob_end_flush () function

Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer

function format: void Ob_end_flush (void)

Description: This function sends the contents of the output buffer (if any).

Ob_implicit_flush () function

function format: void Ob_implicit_flush ([int flag])

Description: The default is to close the buffer, and when the absolute output is turned on, each script output is sent directly to the browser, eliminating the need to call flush ().

Research on caching technology of static pages in PHP

If your site MySQL database is slow, you need to value the site's cache. Used WordPress friends know that it has a plug-in called WP Super Cache, can be WordPress page in the first generation of the storage as a static page, when requesting this page again, it saves time to read the database. This is the technique that is discussed here.

The first question is how to get the contents of the PHP output. The reason for getting the output is simple, because we can store the contents of the output and give it to him when the visitor comes back.

It is equally simple to achieve these goals. We just call the function Ob_start () before the content output, and then call Ob_get_contents () to get the output after all the output is finished, and then call Ob_end_flush () later to end it, a simple example is as follows:

 
  

Output outside of the PHP tag can be recorded.

I've been recorded.

'?>


Program Run Result:

Output outside of the PHP tag can be recorded. I've been recorded. Output outside of the PHP tag can be recorded. I've been recorded.

Visible, $cache variables save the previous output. That is, we can use the cache to reduce the output of the PHP results.

Sometimes we have the habit of not enabling caching for administrators, and caching for tourists. This time, in fact, the implementation is relatively simple. We can write our own two function caches ($id) and End_cache ($id), representing the start of the cache and the end of the cache, and then the code is as follows (there are three functions omitted):

 
  

Sometimes, sites may create pages that are designed specifically for mobile devices as needed. Well, in this case we should extend the $id a bit. There are many ways to extend this extension, such as adding another parameter that will have the mobile device's page in a different folder than the desktop device, which uses the same $id. Another way is to combine the original $id with the user-agent of the mobile device, MD5 () a bit. I prefer the previous approach. Of course there are other things like that, but the central idea is to set the tag ($id) of the cache to something different, and when the user comes back, you can tell them apart.

There are times when a Web site has multiple user roles that may be cached for the appropriate user. Of course, just follow the above principles.

Ob_start () and Ob_end_flush () are recursively processed. That is, you can call Ob_end_flush () several times Ob_start () before calling. For example:

Sometimes, sites may create pages that are designed specifically for mobile devices as needed. Well, in this case we should extend the $id a bit. There are many ways to extend this extension, such as adding another parameter that will have the mobile device's page in a different folder than the desktop device, which uses the same $id. Another way is to combine the original $id with the user-agent of the mobile device, MD5 () a bit. I prefer the previous approach. Of course there are other things like that, but the central idea is to set the tag ($id) of the cache to something different, and when the user comes back, you can tell them apart.

There are times when a Web site has multiple user roles that may be cached for the appropriate user. Of course, just follow the above principles.

Ob_start () and Ob_end_flush () are recursively processed. That is, you can call Ob_end_flush () several times Ob_start () before calling. For example:

 
  '; Ob_start (); Echo ' Content2 '. '
'; $output 1 = ob_get_contents (); echo $output 1. '
'; Ob_end_flush (); $output 2 = ob_get_contents (); echo $output 2. '
'; Ob_end_flush ();? >

Program Run Result:

Content1content2content2content1content2content2


http://www.bkjia.com/PHPjc/755771.html www.bkjia.com true http://www.bkjia.com/PHPjc/755771.html techarticle The cache is widely used in practice, which can reduce the access to the server database and improve the running speed. At present many CMS Content management system uses the cache mechanism frequently to raise the system ...

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