A good introduction to the PHP Cache class and the introduction and use of PHP Cache functions

Source: Internet
Author: User
Cache is widely used in actual use, which can reduce access to the server database and improve the running speed. Currently, many CMS content management systems frequently use caching mechanisms to improve system operation efficiency. The following is a well-written cache class. you can refer to the caching mechanism and writing method. Ca

Cache is widely used in actual use, which can reduce access to the server database and improve the running speed. Currently, many CMS content management systems frequently use caching mechanisms to improve system operation efficiency. The following is a well-written cache class. you can refer to the caching mechanism and writing method.

The cache. php code is as follows:

 Cachefilevar and the file name $ this-> cachefile // different parameters on the dynamic page correspond to different Cache files, but all Cache files on each dynamic page have the same file name, only the extensions are different $ s = array (". ","/"); $ r = array (" _ "," "); $ this-> cachefilevar = str_replace ($ s, $ r, $ _ SERVER ["SCRIPT_NAME"]). "_". $ _ GET [_ ActionVar _]; $ this-> cachefile = $ this-> cachefilevar. ". ". md5 ($ _ SERVER ["REQUEST_URI"]);} // delete the cache function delete () of the current page/module () {// delete the cache of the current page $ d = dir (_ CachePath _); $ strlen = strlen ($ this-> cachefilevar); // return the current All Cache file groups on the page while (false! ==( $ Entry = $ d-> read () {if (substr ($ entry, 0, $ strlen) ==$ this-> cachefilevar) {if (! Unlink (_ CachePath _. "/". $ entry) {echo "the Cache directory cannot be written"; exit ;}}// determine whether the Cache has been cached and whether the Cache function check () is required () {// if the cache update interval _ ReCacheTime _ if (_ ReCacheTime _ + 0> 0) is set) {// return 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 ;}// return the Cache $ file = _ CachePath _ of the current page _. "/". $ this-> cachefile; // judge the current Whether the page Cache exists and whether the Cache function enables return (file_exists ($ file) and _ CacheEnable _ and! $ Ischange);} // read the Cache function read () {// return the current page's Cache $ file = _ CachePath _. "/". $ this-> cachefile; // read the content of the Cache file if (_ CacheEnable _) return readfile ($ file); else return false ;} // Generate Cache function write ($ output) {// return the current page's Cache $ file = _ CachePath _. "/". $ this-> cachefile; // if the Cache function is enabled if (_ CacheEnable _) {// write the output content 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 _ ReCacheTime _ if (_ ReCacheTime _ + 0> 0) is set {// The last update time of the Cache on the current page $ file = _ CachePath _. "/". $ this-> cachefilevar; $ fp = @ fopen ($ file, 'w'); if (! @ Fwrite ($ fp, time () {echo "the Cache directory cannot be written to"; exit ;}@ fclose ($ fp) ;}}}?>


Class usage:

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


Introduction to PHP Cache functions

For example, some information remains unchanged, but the information that can be changed is stored in the cache to speed up the display speed. this is very valuable. for the so-called cache, the common understanding is that some shared information is stored on the server. It is generated on the same server. when saving the cache, we can specify the next update time. for example, we need to update it once every five minutes to record the last update time, compared with the current time, if it is more than 5 minutes, read the database, replace the update with, otherwise directly read the cache data, of course, the cache needs to be activated by the client user, only once.

Ob_start () function

Ob_start () function: Open the output buffer.

Function format void ob_start (void)

Note: When the buffer zone is activated, all non-file header information from the PHP program is not sent, but stored in the internal buffer zone. To output the buffer content, you can use ob_end_flush () or flush () to output the buffer content.

Flush () function

Function format: flush ()

Note: This function is frequently used and highly efficient.

String ob_get_contents () function

Ob_get_contents: returns the content of the internal buffer.

Function format: string ob_get_contents (void)

Note: This function returns the content in the current buffer. if the output buffer is not activated, FALSE is returned.

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. it is the same as ob_get_contents. if the output buffer is not activated, FALSE is returned.

Ob_end_clean () function

Ob_end_clean: delete the content of the internal buffer and disable the internal buffer.

Function format: void ob_end_clean (void)

Note: This function will not output the content of the internal buffer but delete it.

Ob_end_flush () function

Ob_end_flush: sends the content of the internal buffer to the browser and closes the output buffer.

Function format: void ob_end_flush (void)

Note: This function sends the content of the output buffer (if any ).

Ob_implicit_flush () function

Function format: void ob_implicit_flush ([int flag])

Note: the buffer zone is disabled by default. After absolute output is enabled, the output of each script is directly sent to the browser, and you do not need to call flush ().

PHP static page cache technology research

If your website's MySQL database is slow, you need to pay attention to the website's cache. Anyone who has used WordPress knows that it has a plug-in called WP Super Cache, which can store WordPress pages as static pages when they are generated for the first time. when you request this page again, it saves the time to read the database. This technology is discussed here.

The first question is how to get the PHP output content. The reason for getting the output content is very simple, because we can store the output content and give it to the visitor when they come again.

It is also easy to achieve these goals. We only need to call the function ob_start () before the content is output, and then call ob_get_contents () after all the content is output, and then call ob_end_flush () an example is as follows:

 

Output outside the PHP tag can be recorded.

I have been recorded.

'?>


Program running result:

Output outside the PHP tag can be recorded. I have been recorded. Output outside the PHP tag can be recorded. I have been recorded.

As you can see, the $ cache variable saves the previous output results. That is to say, we can use cache to reduce PHP result output.

Sometimes we have this habit: for administrators, caching is disabled, while for tourists, caching is enabled. At this time, the implementation is also relatively simple. We can write two functions cache ($ id) and end_cache ($ id) by ourselves, indicating the cache start and cache end, respectively, and the code is as follows (three functions are omitted here ):

 

Sometimes, the website may create pages specially designed for mobile devices as needed. In this case, we should extend $ id. There are many methods for this extension, such as adding another parameter to make the pages of mobile devices different from the folders of desktop devices, and these pages use the same $ id. another way is to combine the original $ id with the User-agent of the mobile device. md5 () is enough. I prefer the previous approach. Of course, there must be other similar practices. In short, the central idea is to set the cache tag ($ id) to something different and distinguish them when the user returns, you can.

In addition, a website may have multiple user roles, which may be cached by corresponding users. Of course, you only need to follow the above principles.

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

Sometimes, the website may create pages specially designed for mobile devices as needed. In this case, we should extend $ id. There are many methods for this extension, such as adding another parameter to make the pages of mobile devices different from the folders of desktop devices, and these pages use the same $ id. another way is to combine the original $ id with the User-agent of the mobile device. md5 () is enough. I prefer the previous approach. Of course, there must be other similar practices. In short, the central idea is to set the cache tag ($ id) to something different and distinguish them when the user returns, you can.

In addition, a website may have multiple user roles, which may be cached by corresponding users. Of course, you only need to follow the above principles.

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

 ';ob_start();echo 'content2'.'
';$output1 = ob_get_contents(); echo $output1.'
';ob_end_flush();$output2 = ob_get_contents(); echo $output2.'
';ob_end_flush();?>

Program running result:

content1content2content2content1content2content2


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.