feat: 插件使用说明(usage)+ 页面缓存命中统计

- plugin.json 新增 usage 字段(多行文本),后台插件卡片折叠展示;四个内置插件补齐使用说明
- PageCacheMiddleware 记录命中/未命中计数(按日滚动),仪表盘新增「页面缓存」统计卡(条目数 + 命中率),用于判断前台缓存效果、是否需额外加速
- 文档与测试同步
This commit is contained in:
ak
2026-08-12 03:57:22 +08:00
parent 22df17f02a
commit 01861f8809
10 changed files with 60 additions and 5 deletions
+33
View File
@@ -28,12 +28,16 @@ class PageCacheMiddleware
$ttl = (int) config('blog.page_cache_ttl', 300);
if ($cached = Cache::get($key)) {
self::increment('hits');
return response($cached['body'], 200, [
'Content-Type' => $cached['type'],
'Cache-Control' => 'public, max-age='.$ttl,
]);
}
self::increment('misses');
$response = $next($request);
if ($response->getStatusCode() === 200) {
@@ -61,6 +65,35 @@ class PageCacheMiddleware
return count((array) Cache::get(self::TAG.'_keys', []));
}
/**
* 命中/未命中统计(按日滚动),用于判断前台缓存效果。
*/
public static function stats(): array
{
$hits = (int) Cache::get(self::TAG.'_hits', 0);
$misses = (int) Cache::get(self::TAG.'_misses', 0);
$total = $hits + $misses;
return [
'hits' => $hits,
'misses' => $misses,
'count' => self::count(),
'rate' => $total > 0 ? round($hits / $total * 100, 1) : 0.0,
];
}
public static function resetStats(): void
{
Cache::forget(self::TAG.'_hits');
Cache::forget(self::TAG.'_misses');
}
private static function increment(string $metric): void
{
$key = self::TAG.'_'.$metric;
Cache::put($key, (int) Cache::get($key, 0) + 1, now()->addDay());
}
private static function rememberKey(string $key): void
{
$keys = (array) Cache::get(self::TAG.'_keys', []);
+1
View File
@@ -81,6 +81,7 @@ class PluginManager
'title' => $manifest['title'] ?? $name,
'version' => $manifest['version'] ?? '0.0.0',
'description' => $manifest['description'] ?? '',
'usage' => $manifest['usage'] ?? '',
'author' => $manifest['author'] ?? '',
'enabled' => $this->isEnabled($basename),
'requires' => $requires,
+5
View File
@@ -17,6 +17,8 @@ class BlogStats extends BaseWidget
protected function getStats(): array
{
$cache = \App\Blog\Support\PageCacheMiddleware::stats();
return [
Stat::make('文章', Post::published()->count())
->description('总浏览量 '.number_format((float) Post::sum('views')))
@@ -26,6 +28,9 @@ class BlogStats extends BaseWidget
->icon('heroicon-o-chat-bubble-left-right'),
Stat::make('用户', User::count())
->icon('heroicon-o-users'),
Stat::make('页面缓存', $cache['count'].' 条')
->description('命中率 '.$cache['rate'].'%(命中 '.$cache['hits'].' / 未命中 '.$cache['misses'].',按日滚动)')
->icon('heroicon-o-bolt'),
];
}
}