diff --git a/lib/DirScanner.php b/lib/DirScanner.php index b4207e2..3531a0e 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -620,6 +620,16 @@ Class DirScanner { return $this->scanResults; } + //设置扫描结果,以支持缓存 + public function setScanResults($data) { + $this->scanResults = $data; + } + + //设置tree,以支持缓存 + public function setTreeData($data) { + $this->tree = $data; + } + //获取菜单,扫描结果中的目录结构 public function getMenus($tree = array()) { $results = empty($tree) ? $this->tree : $tree; diff --git a/plugins/Common.php b/plugins/Common.php index 1314e92..6767b83 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -528,4 +528,56 @@ Class Common { return preg_replace("/^(.{3,})\d{4}(.{4})$/i", '$1****$2', $cellphone); } + //保存数据到文件缓存 + //缓存数据格式:{ctime: timestamp, data: anything} + public static function saveCacheToFile($key, $data) { + $cacheData = array( + "ctime" => time(), + "data" => $data, + ); + $jsonData = json_encode($cacheData); + $cacheDir = __DIR__ . '/../runtime/cache/'; + + if (!is_dir($cacheDir)) { + mkdir($cacheDir, 0700, true); + } + + $cache_filename = "{$cacheDir}{$key}.json"; + return file_put_contents($cache_filename, $jsonData); + } + + //从文件缓存读取数据 + //expireSeconds: 缓存失效时间,默认10分钟 + public static function getCacheFromFile($key, $expireSeconds = 600) { + $cacheDir = __DIR__ . '/../runtime/cache/'; + $cache_filename = "{$cacheDir}{$key}.json"; + + if (file_exists($cache_filename)) { + $jsonData = file_get_contents($cache_filename); + $data = json_decode($jsonData, true); + + //如果缓存没有失效 + $now = time(); + if ($now - $data['ctime'] <= $expireSeconds) { + return $data['data']; + }else { + return null; + } + } + + return false; + } + + //删除缓存文件 + public static function cleanFileCache($key) { + $cacheDir = __DIR__ . '/../runtime/cache/'; + $cache_filename = "{$cacheDir}{$key}.json"; + + if (file_exists($cache_filename)) { + return unlink($cache_filename); + } + + return false; + } + } \ No newline at end of file diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 7e7e3f4..5629ef3 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -4,6 +4,7 @@ */ require_once __DIR__ . '/../../../lib/DirScanner.php'; require_once __DIR__ . '/../../../plugins/Parsedown.php'; +require_once __DIR__ . '/../../../plugins/Common.php'; Class SiteController extends Controller { @@ -16,8 +17,30 @@ Class SiteController extends Controller { $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); - $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); - $scanResults = $scanner->getScanResults(); + + //优先从缓存读取数据 + $cacheKey = 'allFilesTree'; + $cachedData = Common::getCacheFromFile($cacheKey); + if (!empty($cachedData)) { + $dirTree = $cachedData; + $scanner->setTreeData($cachedData); + }else { + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + Common::saveCacheToFile($cacheKey, $dirTree); + } + + //优先从缓存读取数据 + $cacheKey = 'allFilesData'; + $cachedData = Common::getCacheFromFile($cacheKey); + if (!empty($cachedData)) { + $scanResults = $cachedData; + $scanner->setScanResults($cachedData); + }else { + $scanResults = $scanner->getScanResults(); + Common::saveCacheToFile($cacheKey, $scanResults); + } + + //获取目录 $menus = $scanner->getMenus(); @@ -62,6 +85,7 @@ Class SiteController extends Controller { $mp3File = $scanner->getDefaultFile('mp3'); } + //翻页支持 $page = $this->get('page', 1); $pageSize = $this->get('limit', 24); @@ -75,8 +99,8 @@ Class SiteController extends Controller { } $viewName = 'index'; $params = compact( - 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', - 'mp3File', 'page', 'pageSize' + 'cateId', 'page', 'pageSize', + 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File' ); return $this->render($viewName, $params, $pageTitle); }