From 679c131604f5f8c5bb37c01e1795eee5af1aa99d Mon Sep 17 00:00:00 2001 From: filesite Date: Tue, 16 Jul 2024 13:37:28 +0800 Subject: [PATCH] add cache for dir snapshot image url --- controller/Controller.php | 2 +- themes/beauty/controller/SiteController.php | 71 +++++++++++---------- 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/controller/Controller.php b/controller/Controller.php index 09cdf1d..402feab 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -306,7 +306,7 @@ Class Controller { } //get cache key - protected function getCacheKey($cateId, $dataType = 'tree', $maxScanDeep = 2) { + protected function getCacheKey($cateId, $dataType = 'tree', $maxScanDeep = 0) { $prefix = FSC::$app['config']['theme']; return "{$prefix}_{$dataType}_{$maxScanDeep}_{$cateId}"; } diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index 609cd15..ac50ebf 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -178,40 +178,47 @@ Class SiteController extends Controller { $code = 0; $msg = '参数不能为空'; }else { - //从缓存数据中获取目录的realpath - $cachedData = Common::getCacheFromFile($cacheId); - if (!empty($cachedData)) { - $realpath = $cachedData[$cateId]['realpath']; - $scanner = new DirScanner(); - $scanner->setWebRoot($this->getCurrentWebroot($realpath)); - $scanner->setRootDir($realpath); - - $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); - $imgFile = $scanner->getSnapshotImage($realpath, $imgExts); - - - //支持视频目录 - if (empty($imgFile)) { - $videoExts = !empty(FSC::$app['config']['supportedVideoExts']) ? FSC::$app['config']['supportedVideoExts'] : array('mp4', 'mov', 'm3u8'); - $firstVideo = $scanner->getSnapshotImage($realpath, $videoExts); - if (!empty($firstVideo)) { - $url = '/img/beauty/video_dir.png'; - - //尝试从缓存数据中获取封面图 - $cacheKey = $firstVideo['id']; - $expireSeconds = 86400*30; //有效期30天 - $cacheSubDir = 'video'; - $cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); - if (!empty($cachedData)) { - $url = $cachedData['snapshot']; + //优先从缓存获取 + $cacheKey = $this->getCacheKey($cateId, 'dirsnap'); + $url = Common::getCacheFromFile($cacheKey); + + if (empty($url)) { + //从缓存数据中获取目录的realpath + $cachedData = Common::getCacheFromFile($cacheId); + if (!empty($cachedData)) { + $realpath = $cachedData[$cateId]['realpath']; + $scanner = new DirScanner(); + $scanner->setWebRoot($this->getCurrentWebroot($realpath)); + $scanner->setRootDir($realpath); + + $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); + $imgFile = $scanner->getSnapshotImage($realpath, $imgExts); + + //支持视频目录 + if (empty($imgFile)) { + $videoExts = !empty(FSC::$app['config']['supportedVideoExts']) ? FSC::$app['config']['supportedVideoExts'] : array('mp4', 'mov', 'm3u8'); + $firstVideo = $scanner->getSnapshotImage($realpath, $videoExts); + if (!empty($firstVideo)) { + $url = '/img/beauty/video_dir.png'; + + //尝试从缓存数据中获取封面图 + $cacheKey_snap = $this->getCacheKey($firstVideo['id'], 'vmeta');; + $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 { - $url = $imgFile['path']; + $code = 0; + $msg = '缓存数据已失效,请刷新网页'; } - }else { - $code = 0; - $msg = '缓存数据已失效,请刷新网页'; } } @@ -261,7 +268,7 @@ Class SiteController extends Controller { $code = 0; $msg = '参数不能为空'; }else { - $cacheKey = $videoId; + $cacheKey = $this->getCacheKey($videoId, 'vmeta'); $expireSeconds = 86400*30; //有效期30天 $cacheSubDir = 'video'; $cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); @@ -287,7 +294,7 @@ Class SiteController extends Controller { $code = 0; $msg = '参数不能为空'; }else { - $cacheKey = $videoId; + $cacheKey = $this->getCacheKey($videoId, 'vmeta'); $cacheSubDir = 'video'; $saved = Common::saveCacheToFile($cacheKey, $metaData, $cacheSubDir); if ($saved !== false) {