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>