Browse Source

index data save improve

master
filesite 2 days ago
parent
commit
6f2c7a8575
  1. 73
      themes/beauty/controller/CommandController.php
  2. 4
      themes/beauty/controller/ListController.php
  3. 4
      themes/beauty/controller/SiteController.php

73
themes/beauty/controller/CommandController.php

@ -9,8 +9,9 @@ require_once __DIR__ . '/../../../plugins/Common.php';
Class CommandController extends Controller { Class CommandController extends Controller {
protected $logPrefix = '[MainBot]'; protected $logPrefix = '[MainBot]';
protected $scanedDirCacheKey = 'MainBotScanedDirs'; protected $scanedDirCacheKey = 'MainBotScanedDirs';
protected $dateIndexCacheKey = 'MainBotDateIndex'; protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
protected $allFilesCacheKey = 'MainBotAllFiles';
public function actionIndex() { public function actionIndex() {
$commands = <<<eof $commands = <<<eof
@ -125,6 +126,8 @@ eof;
$this->scanMediaFiles(); $this->scanMediaFiles();
$this->saveDateIndexIntoCacheFile(); $this->saveDateIndexIntoCacheFile();
$this->saveNoOriginalCtimeFilesIntoFile(); $this->saveNoOriginalCtimeFilesIntoFile();
//缓存所有文件id跟文件信息,便于根据id列表来渲染,并按id首字母分子目录存放,以支持大量文件的场景
$this->saveAllFilesIntoCacheFile();
//while (true) { //while (true) {
//$time = date('Y-m-d H:i:s'); //$time = date('Y-m-d H:i:s');
@ -200,6 +203,8 @@ eof;
$scanIndex ++; $scanIndex ++;
if (!empty($item['filename'])) { if (!empty($item['filename'])) {
//保存所有文件到索引
$this->updateAllFilesCache($item);
//更新年份、月份时间索引 //更新年份、月份时间索引
$this->updateDateIndex($item); $this->updateDateIndex($item);
//更新没有拍摄时间的文件索引 //更新没有拍摄时间的文件索引
@ -292,7 +297,68 @@ eof;
return false; return false;
} }
return Common::saveCacheToFile($cacheKey, $cacheData); $cacheDir = 'index';
//save index keys
$indexKeys = [];
foreach ($cacheData as $year => $item) {
$indexKeys[$year] = array_keys($item);
}
Common::saveCacheToFile("{$cacheKey}_keys", $indexKeys, $cacheDir);
return Common::saveCacheToFile($cacheKey, $cacheData, $cacheDir);
}
protected function updateAllFilesCache($file) {
$cacheKey = $this->allFilesCacheKey;
$cacheData = Common::getCache($cacheKey);
if (empty($cacheData)) {
$cacheData = array();
}
$cacheData[$file['id']] = $file;
return Common::setCache($cacheKey, $cacheData);
}
protected function getFilesByFirstCharcter($files, $dirNum) {
$byFirst = [];
foreach ($files as $id => $item) {
$firstChar = substr($id, 0, 1);
$ascNum = ord($firstChar);
$index = $ascNum % $dirNum;
if (empty($byFirst[$index])) {
$byFirst[$index] = [];
}
$byFirst[$index][$id] = $item;
}
return $byFirst;
}
protected function saveAllFilesIntoCacheFile() {
$cacheKey = $this->allFilesCacheKey;
$cacheData = Common::getCache($cacheKey);
if (empty($cacheData)) {
return false;
}
$total = count($cacheData);
$dirNum = 1;
if ($total > 1000 && $total <= 10000) {
$dirNum = 10;
}else if ($total > 10000){
$dirNum = 100;
}
$filesByFirstChar = $this->getFilesByFirstCharcter($cacheData, $dirNum);
$cacheDir = 'index';
for ($i=1;$i<=$dirNum;$i++) {
Common::saveCacheToFile("{$cacheKey}_{$i}", $filesByFirstChar[$i-1], $cacheDir);
}
return true;
} }
//汇总每个目录下图片、视频、音乐文件数量 //汇总每个目录下图片、视频、音乐文件数量
@ -336,7 +402,8 @@ eof;
return false; return false;
} }
return Common::saveCacheToFile($cacheKey, $cacheData); $cacheDir = 'index';
return Common::saveCacheToFile($cacheKey, $cacheData, $cacheDir);
} }
public function actionTest() { public function actionTest() {

4
themes/beauty/controller/ListController.php

@ -8,7 +8,7 @@ require_once __DIR__ . '/../../../plugins/Common.php';
require_once __DIR__ . '/../../../plugins/Html.php'; require_once __DIR__ . '/../../../plugins/Html.php';
Class ListController extends Controller { Class ListController extends Controller {
protected $dateIndexCacheKey = 'MainBotDateIndex'; protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
public function actionIndex() { public function actionIndex() {
@ -303,7 +303,7 @@ Class ListController extends Controller {
$isAdminIp = Common::isAdminIp($this->getUserIp()); //判断是否拥有管理权限 $isAdminIp = Common::isAdminIp($this->getUserIp()); //判断是否拥有管理权限
//从缓存文件获取按年份、月份归类的索引数据 //从缓存文件获取按年份、月份归类的索引数据
$cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey, 86400*365); $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey, 86400*365, 'index');
$viewName = '//site/index'; //共享视图 $viewName = '//site/index'; //共享视图
$params = compact( $params = compact(

4
themes/beauty/controller/SiteController.php

@ -8,7 +8,7 @@ require_once __DIR__ . '/../../../plugins/Common.php';
require_once __DIR__ . '/../../../plugins/Html.php'; require_once __DIR__ . '/../../../plugins/Html.php';
Class SiteController extends Controller { Class SiteController extends Controller {
protected $dateIndexCacheKey = 'MainBotDateIndex'; protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
public function actionIndex() { public function actionIndex() {
@ -261,7 +261,7 @@ Class SiteController extends Controller {
//从缓存文件获取按年份、月份归类的索引数据 //从缓存文件获取按年份、月份归类的索引数据
$cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey, 86400*365); $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey, 86400*365, 'index');
$viewName = 'index'; $viewName = 'index';
$params = compact( $params = compact(

Loading…
Cancel
Save