Summary of several methods to clear cache in PHP

不言
Release: 2023-03-30 12:10:02
Original
4460 people have browsed it

This article mainly introduces the relevant information summarized by several methods of clearing the cache in PHP. I hope that through this article, everyone can master the methods of clearing the cache. Friends in need can refer to it

PHP Clear Cache Summary of several methods

The project being developed now uses the tp3.1 version. During the development process, we often encounter problems with page caching (especially html caching); after refreshing, the problem still exists The old version of the data will still be the old version of the data after refreshing, and I slowly began to doubt my life, haha; so during the development process, we need to clear the cache in time every time.

There are about three ways to clear the cache (all summarized from actual experience):

First: Add the following two lines to the project's configuration file config.php Code can avoid caching problems

 'TMPL_CACHE_ON' => false,//禁止模板编译缓存 
 'HTML_CACHE_ON' => false,//禁止静态缓存
Copy after login

I won’t explain these two lines of code here;

Two: The cache directory of the TP framework is stored in the folder public_html\App\Runtime. After each development is completed, all files in it are manually deleted
(it feels a bit violent and stupid), but this method is the stupidest, testing and line If you don't have permission, you can't delete the environment;

Third: I wrote my own clear cache class. We can create our own "clear cache" class in the same directory as the business controller (core The idea is to use the cache class that comes with the TP framework to operate. You can take a look at the source code of the TP framework) and clear the cache through url access.

The code is as follows:

// +----------------------------------------------------------------------
// | Copyright (c) 2007-2009 
// +----------------------------------------------------------------------
// $Id: ClearAction.class.php 668 2016-05-03 11:43:12Z chenhaibo $
/**
 +------------------------------------------------------------------------------
 * 清除缓存
 +------------------------------------------------------------------------------
 * @author haibo <chenhaibo0806@163.com>
 * @version $Id: ClearAction.class.php 668 2016-05-03 11:43:12Z chenhaibo $
 +------------------------------------------------------------------------------
 */
class ClearAction extends Action{
/**
+----------------------------------------------------------
* 清除缓存
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
*/
public function clearcache() {
 $_token = isset($_GET[&#39;token&#39;]) ? trim($_GET[&#39;token&#39;]) : &#39;&#39;;
 $_operate = isset($_GET[&#39;operate&#39;]) ? trim($_GET[&#39;operate&#39;]) : &#39;&#39;;
 
 $_option = array();
 if($_operate == &#39;runtime&#39;) $_option[&#39;temp&#39;] = RUNTIME_PATH; //各种缓存数据存放目录
 if($_operate == &#39;cache&#39;) $_option[&#39;temp&#39;] = CACHE_PATH;
 if($_operate == &#39;data&#39;) $_option[&#39;temp&#39;] = DATA_PATH;
 if($_operate == &#39;fields&#39;) $_option[&#39;temp&#39;] = DATA_PATH."/_fields";
 
 import(&#39;Think.Util.Cache.CacheFile&#39;);
 $CacheFile = new CacheFile($_option);
 $CacheFile->clear();
 echo &#39;success&#39;;
 }
Copy after login

The clear function actually deletes cache files.


Enter the address in the browser address bar:

http://test.xxx.cn/Clear-clearcache?operate =fields //Test environment
http://www.xxx.cn/Clear-clearcache?operate=fields //Formal environment

If you have any questions, please leave a message or communicate in the community of this site Discussion, thanks for reading, I hope it helps everyone.

Related recommendations:

ThinkPHP implements one-click cache clearing method

##

The above is the detailed content of Summary of several methods to clear cache in PHP. For more information, please follow other related articles on the PHP Chinese website!

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