diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index 016930e..a67ecbe 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -74,39 +74,13 @@ Class ListController extends Controller { } $scanResults = array($cateId => $currentDir); //重新组装数据 - //header('Content-Type: text/html; charset=utf-8'); - //print_r($currentDir); - //print_r($scanResults); - //exit; - - //获取目录 - $menus = $scanner->getMenus(); - - $titles = array(); - $readmeFile = $scanner->getDefaultReadme(); - if (!empty($readmeFile)) { - if (!empty($readmeFile['sort'])) { - $menus_sorted = explode("\n", $readmeFile['sort']); - } - - $titles = $scanner->getMDTitles($readmeFile['id']); - - $Parsedown = new Parsedown(); - $content = file_get_contents($readmeFile['realpath']); - $htmlReadme = $Parsedown->text($content); - $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); - } - - //排序 - $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); - if (!empty($sortedTree)) { - $menus = $sortedTree['menus']; - $dirTree = $sortedTree['dirTree']; - } + //非首页统一从缓存获取目录数据,有效期 1 小时 + $cacheKey = $this->getCacheKey('all', 'menu', 2); + $menus = Common::getCacheFromFile($cacheKey, 3600); + //获取目录面包屑 - //TODO: 重写此方法,根据scanner->getId()方法来逐级目录生成 $subcate = $scanResults[$cateId]; $breadcrumbs = $this->getBreadcrumbs($currentDir, $cachedParentData, $scanner); @@ -189,7 +163,7 @@ Class ListController extends Controller { } //根据目录结构以及当前目录获取面包屑 - //TODO: 改善每一级目录的链接地址中的cid,利用缓存数据把每一级的缓存cid参数记录下来 + //缓存key统一生成,方便按规则获取上一级目录的缓存cid protected function getBreadcrumbs($currentDir, $scanResults, $scanner) { $webroot = FSC::$app['config']['content_directory']; $arr = explode($webroot, $currentDir['realpath']); diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index bdaf4e5..c4aec73 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -46,31 +46,41 @@ Class SiteController extends Controller { } - //获取目录 - $menus = $scanner->getMenus(); - - $titles = array(); - $readmeFile = $scanner->getDefaultReadme(); - if (!empty($readmeFile)) { - if (!empty($readmeFile['sort'])) { - $menus_sorted = explode("\n", $readmeFile['sort']); + //优先从缓存获取目录数据 + $cacheKey = $this->getCacheKey('all', 'menu', $maxScanDeep); + $menus = Common::getCacheFromFile($cacheKey); + + if (empty($menus)) { + //获取目录 + $menus = $scanner->getMenus(); + + $titles = array(); + $readmeFile = $scanner->getDefaultReadme(); + if (!empty($readmeFile)) { + if (!empty($readmeFile['sort'])) { + $menus_sorted = explode("\n", $readmeFile['sort']); + } + + $titles = $scanner->getMDTitles($readmeFile['id']); + + $Parsedown = new Parsedown(); + $content = file_get_contents($readmeFile['realpath']); + $htmlReadme = $Parsedown->text($content); + $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } - $titles = $scanner->getMDTitles($readmeFile['id']); + //排序 + $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); + if (!empty($sortedTree)) { + $menus = $sortedTree['menus']; + $dirTree = $sortedTree['dirTree']; + } - $Parsedown = new Parsedown(); - $content = file_get_contents($readmeFile['realpath']); - $htmlReadme = $Parsedown->text($content); - $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); + Common::saveCacheToFile($cacheKey, $menus); //保存目录数据 } - //排序 - $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); - if (!empty($sortedTree)) { - $menus = $sortedTree['menus']; - $dirTree = $sortedTree['dirTree']; - } + //当前目录数据 $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId];