Browse Source

add cache-control and etag for video meta api

master
filesite 2 months ago
parent
commit
efcd29faa2
  1. 2
      conf/app.php
  2. 18
      themes/beauty/controller/SiteController.php

2
conf/app.php

@ -64,6 +64,8 @@ $configs = array(
'small_image_client_cache_seconds' => 600, 'small_image_client_cache_seconds' => 600,
//目录封面图在浏览器端缓存时长,单位:秒 //目录封面图在浏览器端缓存时长,单位:秒
'dir_snapshot_client_cache_seconds' => 300, 'dir_snapshot_client_cache_seconds' => 300,
//视频、音乐meta在浏览器端缓存时长,单位:秒
'meta_client_cache_seconds' => 300,
//列表页缩略图尺寸设置 //列表页缩略图尺寸设置
'small_image_min_width' => 360, //缩略图最小宽度设置,以确保清晰度 'small_image_min_width' => 360, //缩略图最小宽度设置,以确保清晰度

18
themes/beauty/controller/SiteController.php

@ -805,6 +805,9 @@ Class SiteController extends Controller {
$msg = 'OK'; $msg = 'OK';
$meta = array(); $meta = array();
$httpStatus = 200;
$customHeaders = array();
$videoId = $this->get('id', ''); $videoId = $this->get('id', '');
if (empty($videoId)) { if (empty($videoId)) {
$code = 0; $code = 0;
@ -816,13 +819,26 @@ Class SiteController extends Controller {
$cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir); $cachedData = Common::getCacheFromFile($cacheKey, $expireSeconds, $cacheSubDir);
if (!empty($cachedData)) { if (!empty($cachedData)) {
$meta = $cachedData; $meta = $cachedData;
//增加客户端缓存header
$etag = md5(json_encode($meta));
$etag_from_client = !empty($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : ''; //get etag from client
if (!empty($etag) && $etag == $etag_from_client) {
$httpStatus = 304;
}
$meta_client_cache_seconds = FSC::$app['config']['meta_client_cache_seconds'];
$customHeaders = array(
"Cache-Control: max-age={$meta_client_cache_seconds}",
"Etag: {$etag}",
);
}else { }else {
$code = 0; $code = 0;
$msg = '此视频无缓存或缓存已过期'; $msg = '此视频无缓存或缓存已过期';
} }
} }
return $this->renderJson(compact('code', 'msg', 'meta')); return $this->renderJson(compact('code', 'msg', 'meta'), $httpStatus, $customHeaders);
} }
//保存视频meta数据到缓存,支持手动生成 //保存视频meta数据到缓存,支持手动生成

Loading…
Cancel
Save