thinkphp implement one-click Purge Cache method

Source: Internet
Author: User
This article mainly introduces the thinkphp implementation of a key to clear the cache method, the need for friends can refer to the next

Many open-source CMS systems have a key to clear the cache, the cache is to reduce the pressure on the server, but also the existence of a cache may make some data can not be updated in real-time, we have to implement a thinkphp to clean up the cache function. The code is as follows:

Thinkphp code that executes in the background:

Get the absolute path to clear directories and directories public function cache () {////The foreground is submitted using Ajax get method, here is the first to determine if ($_post[' type ') {//Get passed the value $type =$   _post[' type '];   Will pass over the value to cut, I am using "-" to cut the $name =explode ('-', $type);   The number of strips to be cut is easy to circulate $count =count ($name) below;    Loop calls the above method for ($i =0; $i < $count; $i + +) {//Get the absolute path of the file $abs _dir=dirname (dirname (dirname (dirname)));    Combined path $PA = $abs _dir. ' Indexruntime ';    $runtime = $abs _dir. ' indexruntime~runtime.php '; if (file_exists ($runtime))//Determine if the file exists {unlink ($runtime);//File Delete}//Call to delete all files under the folder method $this->rmfile ($PA, $na    me[$i]);  }//give the prompt information $this->ajaxreturn (1, ' clear Success ', 1);  }else{$this->display (); }} Public Function Rmfile ($path, $fileName) {//Delete execution method//Remove Space $path = Preg_replace ('/(/) {2,}|{}   {1,}/', '/', $path);  Get the full catalog $path. = $fileName;  Determine if this file is a file directory if (Is_dir ($path)) {//Open file if ($dh = Opendir ($path)) {//Traverse file directory name while ($file = Readdir ($DH)) ! = False) {//delete unlink ($path.      $file);     } Close file Closedir ($DH); }    } }

The front page section code is as follows:

<script type= "Text/javascript" src= "__public__/admin/js/jquery.js" ></script><script type= "test/ JavaScript ">$ (function () {$ (' #button '). Click (function () {if (confirm) (" Confirm that you want to clear the cache? "). ) {var $type =$ (' #type '). Val (), var $mess =$ (' #mess '), $.post (' __url__/clear ', {type: $type},function (data) {alert (" Cache cleanup Success ");}); Else{return (false;});}); </script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.