Home > Article > Backend Development > Summary of methods to clear cache in PHP
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,//禁止静态缓存
I won’t explain these two lines of code here;
Second: TP The cache directory of the 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. If the test and online environment It cannot be deleted without permission;
Third: I wrote the clear cache class myself. We can create our own "clear cache" class in the same directory of the business controller (the core idea is to use The cache class that comes with the TP framework is used to operate. You can take a look at the source code of the TP framework). You can 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['token']) ? trim($_GET['token']) : ''; $_operate = isset($_GET['operate']) ? trim($_GET['operate']) : ''; $_option = array(); if($_operate == 'runtime') $_option['temp'] = RUNTIME_PATH; //各种缓存数据存放目录 if($_operate == 'cache') $_option['temp'] = CACHE_PATH; if($_operate == 'data') $_option['temp'] = DATA_PATH; if($_operate == 'fields') $_option['temp'] = DATA_PATH."/_fields"; import('Think.Util.Cache.CacheFile'); $CacheFile = new CacheFile($_option); $CacheFile->clear(); echo 'success'; }
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
The above is the detailed content of Summary of methods to clear cache in PHP. For more information, please follow other related articles on the PHP Chinese website!