From c94094abe9e37b10a534f3298b488eb887623bee Mon Sep 17 00:00:00 2001 From: filesite Date: Tue, 9 Jul 2024 12:34:33 +0800 Subject: [PATCH] improve cache key --- controller/Controller.php | 6 +++++ themes/beauty/controller/ListController.php | 28 ++++++++++----------- themes/beauty/controller/SiteController.php | 10 +++++--- themes/beauty/views/site/index.php | 2 +- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index b60e379..aaf2fe7 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -305,4 +305,10 @@ Class Controller { 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}"; + } + } diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index d0722c3..016930e 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -36,18 +36,12 @@ Class ListController extends Controller { throw new Exception("缓存数据中找不到当前目录,请返回上一页重新进入!", 404); } - //$scanner->setWebRoot(FSC::$app['config']['content_directory']); $scanner->setWebRoot($this->getCurrentWebroot($currentDir)); $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; //最大扫描目录级数 - $cacheKey = "{$prefix}_ltree_{$maxScanDeep}_{$cateId}"; + $cacheKey = $this->getCacheKey($cateId, 'tree', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($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); if (!empty($cachedData)) { $scanResults = $cachedData; @@ -146,7 +140,7 @@ Class ListController extends Controller { $viewName = '//site/index'; //共享视图 $params = compact( 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', - 'mp3File', 'page', 'pageSize', 'cacheDataId', 'cacheParentDataId' + 'mp3File', 'page', 'pageSize', 'cacheDataId' ); return $this->render($viewName, $params, $pageTitle); } @@ -195,6 +189,7 @@ Class ListController extends Controller { } //根据目录结构以及当前目录获取面包屑 + //TODO: 改善每一级目录的链接地址中的cid,利用缓存数据把每一级的缓存cid参数记录下来 protected function getBreadcrumbs($currentDir, $scanResults, $scanner) { $webroot = FSC::$app['config']['content_directory']; $arr = explode($webroot, $currentDir['realpath']); @@ -205,19 +200,24 @@ Class ListController extends Controller { } $cates = explode('/', $arr[1]); - $parentCate = "{$arr[0]}{$webroot}"; - foreach ($cates as $cate) { + $parentCate = preg_replace("/\/$/", '', "{$arr[0]}{$webroot}"); //删除最后一个斜杠 + foreach ($cates as $index => $cate) { if ($cate == $currentDir['directory']) {break;} - $subcate = "{$parentCate}{$cate}"; + $subcate = "{$parentCate}/{$cate}"; $cateId = $scanner->getId($subcate); + + //下一级子目录的id + $parentCateId = $scanner->getId($parentCate); + $cacheKey = $this->getCacheKey($parentCateId, 'data'); + array_push($breads, [ 'id' => $cateId, 'name' => $cate, - 'url' => $scanResults[$cateId]['path'], + 'url' => "/list/?id={$cateId}&cid={$cacheKey}", ]); - $parentCate = "{$subcate}/"; //记录上一级目录 + $parentCate = $subcate; //记录上一级目录 } //最后一级 diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index b550133..bdaf4e5 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -17,12 +17,14 @@ Class SiteController extends Controller { $scanner = new DirScanner(); $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; //最大扫描目录级数 - $cacheKey = "{$prefix}_tree_{$maxScanDeep}_" . md5(FSC::$app['config']['content_directory']); + $cacheKey = $this->getCacheKey($defaultCateId, 'tree', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($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); if (!empty($cachedData)) { $scanResults = $cachedData; diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index 2f672eb..d9ad1a7 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -61,7 +61,7 @@ eof; foreach ($breadcrumbs as $bread) { if ($bread['id'] != $selectedId) { echo <<{$bread['name']} / + {$bread['name']} / eof; } else { echo <<