Browse Source

add file cache support

master
filesite 5 months ago
parent
commit
971b7b7fa1
  1. 10
      lib/DirScanner.php
  2. 52
      plugins/Common.php
  3. 28
      themes/beauty/controller/SiteController.php

10
lib/DirScanner.php

@ -620,6 +620,16 @@ Class DirScanner {
return $this->scanResults; return $this->scanResults;
} }
//设置扫描结果,以支持缓存
public function setScanResults($data) {
$this->scanResults = $data;
}
//设置tree,以支持缓存
public function setTreeData($data) {
$this->tree = $data;
}
//获取菜单,扫描结果中的目录结构 //获取菜单,扫描结果中的目录结构
public function getMenus($tree = array()) { public function getMenus($tree = array()) {
$results = empty($tree) ? $this->tree : $tree; $results = empty($tree) ? $this->tree : $tree;

52
plugins/Common.php

@ -528,4 +528,56 @@ Class Common {
return preg_replace("/^(.{3,})\d{4}(.{4})$/i", '$1****$2', $cellphone); 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;
}
} }

28
themes/beauty/controller/SiteController.php

@ -4,6 +4,7 @@
*/ */
require_once __DIR__ . '/../../../lib/DirScanner.php'; require_once __DIR__ . '/../../../lib/DirScanner.php';
require_once __DIR__ . '/../../../plugins/Parsedown.php'; require_once __DIR__ . '/../../../plugins/Parsedown.php';
require_once __DIR__ . '/../../../plugins/Common.php';
Class SiteController extends Controller { Class SiteController extends Controller {
@ -16,8 +17,30 @@ Class SiteController extends Controller {
$scanner = new DirScanner(); $scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']); $scanner->setWebRoot(FSC::$app['config']['content_directory']);
//优先从缓存读取数据
$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); $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(); $scanResults = $scanner->getScanResults();
Common::saveCacheToFile($cacheKey, $scanResults);
}
//获取目录 //获取目录
$menus = $scanner->getMenus(); $menus = $scanner->getMenus();
@ -62,6 +85,7 @@ Class SiteController extends Controller {
$mp3File = $scanner->getDefaultFile('mp3'); $mp3File = $scanner->getDefaultFile('mp3');
} }
//翻页支持 //翻页支持
$page = $this->get('page', 1); $page = $this->get('page', 1);
$pageSize = $this->get('limit', 24); $pageSize = $this->get('limit', 24);
@ -75,8 +99,8 @@ Class SiteController extends Controller {
} }
$viewName = 'index'; $viewName = 'index';
$params = compact( $params = compact(
'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'cateId', 'page', 'pageSize',
'mp3File', 'page', 'pageSize' 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File'
); );
return $this->render($viewName, $params, $pageTitle); return $this->render($viewName, $params, $pageTitle);
} }

Loading…
Cancel
Save