Browse Source

improve cache key

master
filesite 5 months ago
parent
commit
c94094abe9
  1. 6
      controller/Controller.php
  2. 28
      themes/beauty/controller/ListController.php
  3. 10
      themes/beauty/controller/SiteController.php
  4. 2
      themes/beauty/views/site/index.php

6
controller/Controller.php

@ -305,4 +305,10 @@ Class Controller {
return array_pop($arr); return array_pop($arr);
} }
//get cache key
protected function getCacheKey($cateId, $dataType = 'tree', $maxScanDeep = 2) {
$prefix = FSC::$app['config']['theme'];
return "{$prefix}_{$dataType}_{$maxScanDeep}_{$cateId}";
}
} }

28
themes/beauty/controller/ListController.php

@ -36,18 +36,12 @@ Class ListController extends Controller {
throw new Exception("缓存数据中找不到当前目录,请返回上一页重新进入!", 404); throw new Exception("缓存数据中找不到当前目录,请返回上一页重新进入!", 404);
} }
//$scanner->setWebRoot(FSC::$app['config']['content_directory']);
$scanner->setWebRoot($this->getCurrentWebroot($currentDir)); $scanner->setWebRoot($this->getCurrentWebroot($currentDir));
$scanner->setRootDir($currentDir['realpath']); $scanner->setRootDir($currentDir['realpath']);
//header('Content-Type: text/html; charset=utf-8');
//echo $this->getCurrentWebroot($currentDir) . "\n";
//echo $currentDir['realpath'] . "\n";
//exit;
//优先从缓存读取数据 //优先从缓存读取数据
$prefix = FSC::$app['config']['theme'] . '_v1.0';
$maxScanDeep = 2; //最大扫描目录级数 $maxScanDeep = 2; //最大扫描目录级数
$cacheKey = "{$prefix}_ltree_{$maxScanDeep}_{$cateId}"; $cacheKey = $this->getCacheKey($cateId, 'tree', $maxScanDeep);
$cachedData = Common::getCacheFromFile($cacheKey); $cachedData = Common::getCacheFromFile($cacheKey);
if (!empty($cachedData)) { if (!empty($cachedData)) {
$dirTree = $cachedData; $dirTree = $cachedData;
@ -58,7 +52,7 @@ Class ListController extends Controller {
} }
//优先从缓存读取数据 //优先从缓存读取数据
$cacheKey = $cacheDataId = "{$prefix}_ldata_{$maxScanDeep}_{$cateId}"; $cacheKey = $cacheDataId = $this->getCacheKey($cateId, 'data', $maxScanDeep);
$cachedData = Common::getCacheFromFile($cacheKey); $cachedData = Common::getCacheFromFile($cacheKey);
if (!empty($cachedData)) { if (!empty($cachedData)) {
$scanResults = $cachedData; $scanResults = $cachedData;
@ -146,7 +140,7 @@ Class ListController extends Controller {
$viewName = '//site/index'; //共享视图 $viewName = '//site/index'; //共享视图
$params = compact( $params = compact(
'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme',
'mp3File', 'page', 'pageSize', 'cacheDataId', 'cacheParentDataId' 'mp3File', 'page', 'pageSize', 'cacheDataId'
); );
return $this->render($viewName, $params, $pageTitle); return $this->render($viewName, $params, $pageTitle);
} }
@ -195,6 +189,7 @@ Class ListController extends Controller {
} }
//根据目录结构以及当前目录获取面包屑 //根据目录结构以及当前目录获取面包屑
//TODO: 改善每一级目录的链接地址中的cid,利用缓存数据把每一级的缓存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']);
@ -205,19 +200,24 @@ Class ListController extends Controller {
} }
$cates = explode('/', $arr[1]); $cates = explode('/', $arr[1]);
$parentCate = "{$arr[0]}{$webroot}"; $parentCate = preg_replace("/\/$/", '', "{$arr[0]}{$webroot}"); //删除最后一个斜杠
foreach ($cates as $cate) { foreach ($cates as $index => $cate) {
if ($cate == $currentDir['directory']) {break;} if ($cate == $currentDir['directory']) {break;}
$subcate = "{$parentCate}{$cate}"; $subcate = "{$parentCate}/{$cate}";
$cateId = $scanner->getId($subcate); $cateId = $scanner->getId($subcate);
//下一级子目录的id
$parentCateId = $scanner->getId($parentCate);
$cacheKey = $this->getCacheKey($parentCateId, 'data');
array_push($breads, [ array_push($breads, [
'id' => $cateId, 'id' => $cateId,
'name' => $cate, 'name' => $cate,
'url' => $scanResults[$cateId]['path'], 'url' => "/list/?id={$cateId}&cid={$cacheKey}",
]); ]);
$parentCate = "{$subcate}/"; //记录上一级目录 $parentCate = $subcate; //记录上一级目录
} }
//最后一级 //最后一级

10
themes/beauty/controller/SiteController.php

@ -17,12 +17,14 @@ Class SiteController extends Controller {
$scanner = new DirScanner(); $scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']); $scanner->setWebRoot(FSC::$app['config']['content_directory']);
$scanner->setRootDir(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']);
$rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'];
$scanner->setRootDir($rootDir);
//优先从缓存读取数据 //优先从缓存读取数据
$prefix = FSC::$app['config']['theme']; $defaultCateId = $scanner->getId(preg_replace("/\/$/", '', realpath($rootDir)));
$maxScanDeep = 2; //最大扫描目录级数 $maxScanDeep = 2; //最大扫描目录级数
$cacheKey = "{$prefix}_tree_{$maxScanDeep}_" . md5(FSC::$app['config']['content_directory']); $cacheKey = $this->getCacheKey($defaultCateId, 'tree', $maxScanDeep);
$cachedData = Common::getCacheFromFile($cacheKey); $cachedData = Common::getCacheFromFile($cacheKey);
if (!empty($cachedData)) { if (!empty($cachedData)) {
$dirTree = $cachedData; $dirTree = $cachedData;
@ -33,7 +35,7 @@ Class SiteController extends Controller {
} }
//优先从缓存读取数据 //优先从缓存读取数据
$cacheKey = $cacheDataId = "{$prefix}_data_{$maxScanDeep}_" . md5(FSC::$app['config']['content_directory']); $cacheKey = $cacheDataId = $this->getCacheKey($defaultCateId, 'data', $maxScanDeep);
$cachedData = Common::getCacheFromFile($cacheKey); $cachedData = Common::getCacheFromFile($cacheKey);
if (!empty($cachedData)) { if (!empty($cachedData)) {
$scanResults = $cachedData; $scanResults = $cachedData;

2
themes/beauty/views/site/index.php

@ -61,7 +61,7 @@ eof;
foreach ($breadcrumbs as $bread) { foreach ($breadcrumbs as $bread) {
if ($bread['id'] != $selectedId) { if ($bread['id'] != $selectedId) {
echo <<<eof echo <<<eof
<a href="{$bread['url']}&cid={$viewData['cacheParentDataId']}">{$bread['name']}</a> / <a href="{$bread['url']}">{$bread['name']}</a> /
eof; eof;
} else { } else {
echo <<<eof echo <<<eof

Loading…
Cancel
Save