Home > Backend Development > PHP Tutorial > Use the built-in PHP function to output cache_PHP tutorial

Use the built-in PHP function to output cache_PHP tutorial

WBOY
Release: 2016-07-13 10:33:17
Original
1059 people have browsed it

If your website’s MySQL database is slow, you need to pay attention to the website’s cache. Friends who have used WordPress know that it has a plug-in called WP Super Cache, which can store WordPress pages as static pages when they are first generated. When the page is requested again, it saves the time of reading the database. It is this technique that is discussed here.

The first question is how to obtain the content output by PHP. The reason for obtaining the output content is simple, because we can store the output content and give the visitor the pre-saved content when he comes again.

Achieving these goals is just as easy. We only need to call the function ob_start() before the content is output, then call ob_get_contents() after all the content is output to obtain the output content, and then call ob_end_flush() to indicate the end. A simple example is as follows:

<?php ob_start(); ?>
<p>在 PHP 标签之外的输出可以被记录。</p>
<?php echo '<p>我被记录了。</p>' ?>
<?php $cache = ob_get_contents(); ?>
<?php
	/* 在这里添加任何处理 $cache 的代码 */
	echo $cache;
?>
<?php ob_end_flush(); ?>
Copy after login

Program execution result:

在 PHP 标签之外的输出可以被记录。
我被记录了。
在 PHP 标签之外的输出可以被记录。
我被记录了。
Copy after login

It can be seen that the $cache variable saves the previous output results. That is to say, we can reduce PHP's result output through cache.

Sometimes we have the habit of not enabling caching for administrators, but enabling caching for visitors. At this time, it is actually relatively simple to implement. We can write two functions cache($id) and end_cache($id) ourselves, which represent the start of cache and the end of cache respectively, and then the code is as follows (three functions are omitted here):

<?php
function is_admin() {
  /* 该函数用于测试当前用户是否是管理员,若是管理员则返回 true. */
}
function show_cache($id) {
  /* 根据 $id 读取并显示缓存内容,若无缓存则返回 false. */
}
function save_cache($id, $content) {
  /* 将标识符为 $id 的缓存,以内容 $content 储存。 */
}
function cache($id) {
  if (is_admin())
    return false;
  if (show_cache($id))
    return false;
  ob_start();
  return true;
}
function end_cache($id) {
  if (is_admin())
    return false;
  save_cache($id, ob_get_contents());
  ob_end_flush();
  return true;
}
?>
Copy after login

Sometimes, the site may establish pages specifically designed for mobile devices as needed. So, in this case we should expand $id. There are many ways to extend this, such as adding another parameter to store the mobile device pages in a different folder than the desktop device, and these pages use the same $id. Another way is to change the original $id Combined with the User-agent of mobile devices, md5() is all it takes. I prefer the former approach. Of course, there must be other similar approaches. In short, the central idea is to set the cache tag ($id) to something different, and when the user comes back, he can distinguish them. That's it.

Sometimes, a website has multiple user roles, and corresponding caches may need to be given to corresponding users. Of course, just follow the principles above.

ob_start() and ob_end_flush() are processed recursively. In other words, you can call ob_start() several times before calling ob_end_flush(). For example:

<?php
ob_start();
echo 'content1'.'<br />';
ob_start();
echo 'content2'.'<br />';
$output1 = ob_get_contents(); 
echo $output1.'<br />';
ob_end_flush();
$output2 = ob_get_contents(); 
echo $output2.'<br />';
ob_end_flush();
?>
Copy after login

Program execution result:

content1
content2
content2
content1
content2
content2
Copy after login

$output2 caches the previous three outputs.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752517.htmlTechArticleIf your website’s MySQL database is slow, you need to pay attention to the website’s cache. Friends who have used WordPress know that it has a plug-in called WP Super Cache, which can store Wo...
Related labels:
php
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