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;
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['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
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!