Browse Source

add cache for dir snapshot image url

master
filesite 4 months ago
parent
commit
679c131604
  1. 2
      controller/Controller.php
  2. 71
      themes/beauty/controller/SiteController.php

2
controller/Controller.php

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

71
themes/beauty/controller/SiteController.php

@ -178,40 +178,47 @@ Class SiteController extends Controller {
$code = 0; $code = 0;
$msg = '参数不能为空'; $msg = '参数不能为空';
}else { }else {
//从缓存数据中获取目录的realpath //优先从缓存获取
$cachedData = Common::getCacheFromFile($cacheId); $cacheKey = $this->getCacheKey($cateId, 'dirsnap');
if (!empty($cachedData)) { $url = Common::getCacheFromFile($cacheKey);
$realpath = $cachedData[$cateId]['realpath'];
$scanner = new DirScanner(); if (empty($url)) {
$scanner->setWebRoot($this->getCurrentWebroot($realpath)); //从缓存数据中获取目录的realpath
$scanner->setRootDir($realpath); $cachedData = Common::getCacheFromFile($cacheId);
if (!empty($cachedData)) {
$imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); $realpath = $cachedData[$cateId]['realpath'];
$imgFile = $scanner->getSnapshotImage($realpath, $imgExts); $scanner = new DirScanner();
$scanner->setWebRoot($this->getCurrentWebroot($realpath));
$scanner->setRootDir($realpath);
//支持视频目录
if (empty($imgFile)) { $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif');
$videoExts = !empty(FSC::$app['config']['supportedVideoExts']) ? FSC::$app['config']['supportedVideoExts'] : array('mp4', 'mov', 'm3u8'); $imgFile = $scanner->getSnapshotImage($realpath, $imgExts);
$firstVideo = $scanner->getSnapshotImage($realpath, $videoExts);
if (!empty($firstVideo)) { //支持视频目录
$url = '/img/beauty/video_dir.png'; if (empty($imgFile)) {
$videoExts = !empty(FSC::$app['config']['supportedVideoExts']) ? FSC::$app['config']['supportedVideoExts'] : array('mp4', 'mov', 'm3u8');
//尝试从缓存数据中获取封面图 $firstVideo = $scanner->getSnapshotImage($realpath, $videoExts);
$cacheKey = $firstVideo['id']; if (!empty($firstVideo)) {
$expireSeconds = 86400*30; //有效期30天 $url = '/img/beauty/video_dir.png';
$cacheSubDir = 'video';
$cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); //尝试从缓存数据中获取封面图
if (!empty($cachedData)) { $cacheKey_snap = $this->getCacheKey($firstVideo['id'], 'vmeta');;
$url = $cachedData['snapshot']; $expireSeconds = 86400*30; //有效期30天
$cacheSubDir = 'video';
$cachedData = Common::getCacheFromFile($cacheKey_snap, $expireSeconds, $cacheSubDir);
if (!empty($cachedData)) {
$url = $cachedData['snapshot'];
Common::saveCacheToFile($cacheKey, $url);
}
} }
}else {
$url = $imgFile['path'];
Common::saveCacheToFile($cacheKey, $url);
} }
}else { }else {
$url = $imgFile['path']; $code = 0;
$msg = '缓存数据已失效,请刷新网页';
} }
}else {
$code = 0;
$msg = '缓存数据已失效,请刷新网页';
} }
} }
@ -261,7 +268,7 @@ Class SiteController extends Controller {
$code = 0; $code = 0;
$msg = '参数不能为空'; $msg = '参数不能为空';
}else { }else {
$cacheKey = $videoId; $cacheKey = $this->getCacheKey($videoId, 'vmeta');
$expireSeconds = 86400*30; //有效期30天 $expireSeconds = 86400*30; //有效期30天
$cacheSubDir = 'video'; $cacheSubDir = 'video';
$cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); $cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir);
@ -287,7 +294,7 @@ Class SiteController extends Controller {
$code = 0; $code = 0;
$msg = '参数不能为空'; $msg = '参数不能为空';
}else { }else {
$cacheKey = $videoId; $cacheKey = $this->getCacheKey($videoId, 'vmeta');
$cacheSubDir = 'video'; $cacheSubDir = 'video';
$saved = Common::saveCacheToFile($cacheKey, $metaData, $cacheSubDir); $saved = Common::saveCacheToFile($cacheKey, $metaData, $cacheSubDir);
if ($saved !== false) { if ($saved !== false) {

Loading…
Cancel
Save