diff --git a/db/update18.4.beta1.sql b/db/update18.4.beta1.sql new file mode 100644 index 0000000000..9f4584d725 --- /dev/null +++ b/db/update18.4.beta1.sql @@ -0,0 +1 @@ +INSERT INTO `zt_cron` (`m`, `h`, `dom`, `mon`, `dow`, `command`, `remark`, `type`, `buildin`, `status`) VALUES ('0','*','*','*','*','moduleName=misc&methodName=cleanCache', '清理缓存文件','zentao', 1, 'normal'); diff --git a/db/zentao.sql b/db/zentao.sql index 945b0ebd8a..8dd9819e5d 100755 --- a/db/zentao.sql +++ b/db/zentao.sql @@ -2122,7 +2122,8 @@ INSERT INTO `zt_cron` (`m`, `h`, `dom`, `mon`, `dow`, `command`, `remark`, `type ('*/5', '*', '*', '*', '*', 'moduleName=ci&methodName=checkCompileStatus', '同步DevOps构建任务状态', 'zentao', 1, 'normal'), ('*/5', '*', '*', '*', '*', 'moduleName=ci&methodName=exec', '执行DevOps构建任务', 'zentao', 1, 'normal'), ('*/5', '*', '*', '*', '*', 'moduleName=mr&methodName=syncMR', '定时同步GitLab合并数据到禅道数据库', 'zentao', 1, 'normal'), -('*/5', '*', '*', '*', '*', 'moduleName=compile&methodName=syncCompile', '定时同步构建记录', 'zentao', 1, 'normal'); +('*/5', '*', '*', '*', '*', 'moduleName=compile&methodName=syncCompile', '定时同步构建记录', 'zentao', 1, 'normal'), +('0', '*', '*', '*', '*', 'moduleName=misc&methodName=cleanCache', '清理缓存文件', 'zentao', 1, 'normal'); INSERT INTO `zt_group` (`vision`, `name`, `role`, `desc`) VALUES ('rnd', 'ADMIN', 'admin', 'for administrator'), diff --git a/module/misc/control.php b/module/misc/control.php index ceabc1ab7b..39d41daa2e 100644 --- a/module/misc/control.php +++ b/module/misc/control.php @@ -338,4 +338,23 @@ class misc extends control if(strpos(",$accounts,", $this->app->user->account) === false) $accounts .= ',' . $this->app->user->account; $this->loadModel('setting')->setItem('system.common.global.skip' . ucfirst($feature), $accounts); } + + /** + * Clean cache files. + * + * @return void + */ + public function cleanCache() + { + $cacheConfig = $this->config->cache; + if(!$cacheConfig->enable && !$cacheConfig->enableFullPage) + { + echo 'Cache is disabled.'; + return; + } + + $this->misc->getCacheFiles(rtrim($this->app->getCacheRoot(), DS)); + + echo 'Cleaned cache files.'; + } } diff --git a/module/misc/model.php b/module/misc/model.php index 47405960ad..0258444862 100644 --- a/module/misc/model.php +++ b/module/misc/model.php @@ -134,4 +134,36 @@ class miscModel extends model return $weakSites; } + + /** + * Get cache files. + * + * @param string $directory + * @return void + */ + public function getCacheFiles($directory) + { + $files = glob($directory . DS . '*.cache'); + + foreach($files as $file) $this->readCacheFile($file); + + $subdirectories = glob($directory . DS . '*', GLOB_ONLYDIR); + + foreach($subdirectories as $subdirectory) $this->getCacheFiles($subdirectory); + } + + /** + * Read cache file content, and delete it if expired. + * + * @param string $file + * @return bool + */ + public function readCacheFile($file) + { + $content = file_get_contents($file); + $content = unserialize($content); + + if(is_null($content['time'])) return false; + if(time() > $content['time']) return unlink($file); + } }