Share several ways to clear cache in php

黄舟
Release: 2023-03-16 06:46:01
Original
4786 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 The code can avoid caching problems

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

I will not explain these two lines of code here;

Second: The cache directory of the TP framework is stored in the folder public_html\App\ In runtime, manually delete all files inside after each development is completed
(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 TP framework You can take a look at the source code) 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  //测试环境
http://www.xxx.cn/Clear-clearcache?operate=fields  //正式环境
Copy after login

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