TP5 of thinkPHP Framework Implementation Background Cache Cleaning Function Example
- 2021-12-11 17:27:01
- OfStack
This article describes the example of TP5 (thinkPHP framework) to achieve background cache clearing function. Share it for your reference, as follows:
layui plug-in http://www.layui.com/
1--common files
/**
* Delete directories and files cyclically
* @param string $dir_name
* @return bool
*/
function delete_dir_file($dir_name) {
$result = false;
if(is_dir($dir_name)){
if ($handle = opendir($dir_name)) {
while (false !== ($item = readdir($handle))) {
if ($item != '.' && $item != '..') {
if (is_dir($dir_name . DS . $item)) {
delete_dir_file($dir_name . DS . $item);
} else {
unlink($dir_name . DS . $item);
}
}
}
closedir($handle);
if (rmdir($dir_name)) {
$result = true;
}
}
}
return $result;
}
2-From the controller
/**
* Clear the cache
*/
public function clear() {
if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
$this->success(' Cache clearing succeeded ');
} else {
$this->error(' Cache clearing failed ');
}
}
3-html code
<a href="javascript::void(0)" rel="external nofollow" onclick="clearPhp(this)" data-GetUrl="{:url('login/clear')}"> Clear cache </a>
4--js code
<script>
function clearPhp(obj) {
var url=obj.getAttribute('data-GetUrl');
// Inquiry box
layer.confirm(' Are you sure you want to clear it? ', {
btn: [' Determine ',' Cancel '] // Button
},
function(){
$.get(url,function(info){
if(info.code === 1){
setTimeout(function () {location.href = info.url;}, 1000);
}
layer.msg(info.msg);
});
},
function(){});
}
</script>
Additional features and plug-in addresses: https://www.kancloud.cn/he_he/thinkphp5
For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.