From c86dc055728ee946449c01340afc6f3c8993b8f1 Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 25 Sep 2016 14:21:51 +0800 Subject: [PATCH] add options of caching things --- .../Middleware/CheckPlayerExistMiddleware.php | 10 +- app/Listeners/CachePlayerExists.php | 9 +- .../ResponseMacroServiceProvider.php | 4 +- config/options.php | 6 +- resources/views/admin/options.tpl | 94 +++++++++++++++---- 5 files changed, 101 insertions(+), 22 deletions(-) diff --git a/app/Http/Middleware/CheckPlayerExistMiddleware.php b/app/Http/Middleware/CheckPlayerExistMiddleware.php index a4191277..ca183dea 100644 --- a/app/Http/Middleware/CheckPlayerExistMiddleware.php +++ b/app/Http/Middleware/CheckPlayerExistMiddleware.php @@ -21,7 +21,15 @@ class CheckPlayerExistMiddleware Event::fire(new CheckPlayerExists($player_name)); if (PlayerModel::where('player_name', $player_name)->get()->isEmpty()) { - abort(404, trans('general.unexistent-player')); + if (option('return_200_when_notfound') == "1") { + return json([ + 'player_name' => $player_name, + 'errno' => 404, + 'msg' => 'Player Not Found.' + ]); + } else { + abort(404, trans('general.unexistent-player')); + } } return $next($request); diff --git a/app/Listeners/CachePlayerExists.php b/app/Listeners/CachePlayerExists.php index 99f967c2..9c18103e 100644 --- a/app/Listeners/CachePlayerExists.php +++ b/app/Listeners/CachePlayerExists.php @@ -25,7 +25,14 @@ class CachePlayerExists Storage::disk('cache')->put("notfound/$player_name", ''); } } else { - abort(404, '角色不存在'); + if (option('return_200_when_notfound') == "1") { + return json([ + 'player_name' => $player_name, + 'message' => 'Player Not Found.' + ]); + } else { + abort(404, trans('general.unexistent-player')); + } } } } diff --git a/app/Providers/ResponseMacroServiceProvider.php b/app/Providers/ResponseMacroServiceProvider.php index 279ca3d1..2eee1913 100644 --- a/app/Providers/ResponseMacroServiceProvider.php +++ b/app/Providers/ResponseMacroServiceProvider.php @@ -34,8 +34,8 @@ class ResponseMacroServiceProvider extends ServiceProvider }, $status, array_merge([ 'Content-type' => 'image/png', 'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified).' GMT', - 'Cache-Control' => 'public, max-age=31536000', // 365 days - 'Expires' => gmdate('D, d M Y H:i:s', $last_modified + 31536000).' GMT', + 'Cache-Control' => 'public, max-age='.option('cache_expire_time'), // 365 days + 'Expires' => gmdate('D, d M Y H:i:s', $last_modified + option('cache_expire_time')).' GMT', 'Etag' => $etag ], $header)); }); diff --git a/config/options.php b/config/options.php index db78f022..3c7c800f 100644 --- a/config/options.php +++ b/config/options.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-07-29 11:53:11 * @Last Modified by: printempw - * @Last Modified time: 2016-09-25 09:32:25 + * @Last Modified time: 2016-09-25 13:31:41 */ return [ @@ -33,5 +33,7 @@ return [ 'check_update' => '1', 'update_source' => 'nyavm', 'copyright_text' => 'Copyright © 2016 {site_name}. All rights reserved.', - 'auto_del_invalid_texture' => '0' + 'auto_del_invalid_texture' => '0', + 'return_200_when_notfound' => '0', + 'cache_expire_time' => '31536000' ]; diff --git a/resources/views/admin/options.tpl b/resources/views/admin/options.tpl index c0fd634e..bb466b9d 100644 --- a/resources/views/admin/options.tpl +++ b/resources/views/admin/options.tpl @@ -35,11 +35,16 @@
$value) { // remove slash if site_url is ended with slash @@ -109,17 +114,6 @@ - - 头像缓存 - - - - - - - 失效材质 @@ -174,6 +168,74 @@
+
+
+

缓存相关配置 + +

+
+
+ +
+ $value) { + // remove slash if site_url is ended with slash + if ($key == "site_url" && substr($value, -1) == "/") + $value = substr($value, 0, -1); + + if ($key != "option" && $key != "submit") + Option::set($key, $value); + } + echo '
设置已保存。
'; + } ?> + + + + + + + + + + + + + + + + + +
头像缓存 + +
HTTP 响应码 + +
缓存失效时间 + + + +
+
+ +
+
+

数据对接配置