false" to the project's configuration file config.php to avoid cache problems; 2. Delete all files stored in the public folder."/> false" to the project's configuration file config.php to avoid cache problems; 2. Delete all files stored in the public folder.">

Home  >  Article  >  Backend Development  >  How to clear cache when php exits

How to clear cache when php exits

hzc
hzcOriginal
2020-06-05 11:17:132830browse

How to clear cache when php exits

How to clear the cache when php exits :

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

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 to avoid cache problems.

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

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, which needs to be manually updated after each development. Delete all the files inside

(it feels a bit violent and stupid), but this method is the stupidest. Test and online environments cannot be deleted without permission;

Third: Myself To write a 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. You can take a look at the source code of the TP framework). Clear the cache through url access. The code is as follows:

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';
  }
}
clear函数其实就是删除缓存文件。

How to clear cache when php exits

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

Basically these Well, I hope it helps everyone.

Recommended tutorial: "php tutorial"

The above is the detailed content of How to clear cache when php exits. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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