Browse Source

add cache data for menus

master
filesite 5 months ago
parent
commit
05a46f9c94
  1. 36
      themes/beauty/controller/ListController.php
  2. 48
      themes/beauty/controller/SiteController.php

36
themes/beauty/controller/ListController.php

@ -74,39 +74,13 @@ Class ListController extends Controller {
} }
$scanResults = array($cateId => $currentDir); //重新组装数据 $scanResults = array($cateId => $currentDir); //重新组装数据
//header('Content-Type: text/html; charset=utf-8');
//print_r($currentDir);
//print_r($scanResults);
//exit;
//非首页统一从缓存获取目录数据,有效期 1 小时
//获取目录 $cacheKey = $this->getCacheKey('all', 'menu', 2);
$menus = $scanner->getMenus(); $menus = Common::getCacheFromFile($cacheKey, 3600);
$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'];
}
//获取目录面包屑 //获取目录面包屑
//TODO: 重写此方法,根据scanner->getId()方法来逐级目录生成
$subcate = $scanResults[$cateId]; $subcate = $scanResults[$cateId];
$breadcrumbs = $this->getBreadcrumbs($currentDir, $cachedParentData, $scanner); $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) { protected function getBreadcrumbs($currentDir, $scanResults, $scanner) {
$webroot = FSC::$app['config']['content_directory']; $webroot = FSC::$app['config']['content_directory'];
$arr = explode($webroot, $currentDir['realpath']); $arr = explode($webroot, $currentDir['realpath']);

48
themes/beauty/controller/SiteController.php

@ -46,31 +46,41 @@ Class SiteController extends Controller {
} }
//获取目录 //优先从缓存获取目录数据
$menus = $scanner->getMenus(); $cacheKey = $this->getCacheKey('all', 'menu', $maxScanDeep);
$menus = Common::getCacheFromFile($cacheKey);
$titles = array();
$readmeFile = $scanner->getDefaultReadme(); if (empty($menus)) {
if (!empty($readmeFile)) { //获取目录
if (!empty($readmeFile['sort'])) { $menus = $scanner->getMenus();
$menus_sorted = explode("\n", $readmeFile['sort']);
$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(); Common::saveCacheToFile($cacheKey, $menus); //保存目录数据
$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'];
}
//当前目录数据
$cateId = $this->get('id', $menus[0]['id']); $cateId = $this->get('id', $menus[0]['id']);
$subcate = $scanResults[$cateId]; $subcate = $scanResults[$cateId];

Loading…
Cancel
Save