Home > Backend Development > PHP Tutorial > Share several ways to clear cache in PHP

Share several ways to clear cache in PHP

小云云
Release: 2023-03-19 16:18:01
Original
3097 people have browsed it

The project being developed now uses the tp3.1 version. During the development process, we often encounter problems with page caching (especially the cache of html); after refreshing, the data is still the old version, and the data is still the old version after refreshing. Slowly I started 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 of code to the project's configuration file config.php. Avoid caching problems

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

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

Second: The cache directory of the TP framework is stored in the folder public_html\App\Runtime. Manually delete all the files inside after each development
(it feels a bit violent and stupid), but this method is the stupidest. Testing and online environments cannot be deleted without permission;

Third: I wrote my own clear cache class. We can create our own "clear cache" class in the same directory as the business controller (the core idea is to use the cache class that comes with the TP framework to operate, the source code of the TP framework You can see below) to 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 the cache file.


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

Related recommendations:

Summary of methods to clear cache in PHP

Sample code sharing of Javascript how to obtain cache and clear cache API

php fails to draw pictures , using clear cache ob_clean can be solved.

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

Related labels:
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