1919
2020class BlogController extends Controller
2121{
22+ const CACHE_KEY_ARTICLE_RECOMMEND = 'article_recommendations_cache ' ;
23+
2224 // 新增分类
2325 public function storeCategory (StoreBlogCategoryRequest $ request )
2426 {
@@ -91,6 +93,8 @@ public function storeArticle(StoreBlogArticleRequest $request)
9193
9294 $ article = BlogArticle::create ($ validated );
9395
96+ Cache::tags ([self ::CACHE_KEY_ARTICLE_RECOMMEND ])->flush ();
97+
9498 return response ()->json ([
9599 'success ' => true ,
96100 'data ' => $ article ,
@@ -266,7 +270,6 @@ public function recommendArticles(Request $request)
266270 'type ' => 'nullable|in:latest,popular '
267271 ]);
268272
269- $ cacheKey = 'article_recommendations_ ' . $ validated ['type ' ] . '_ ' . md5 (json_encode ($ validated ));
270273 $ expiresAt = now ()->addHours (1 ); // 缓存2小时
271274 $ fields = [
272275 'id ' ,
@@ -283,7 +286,7 @@ public function recommendArticles(Request $request)
283286 'seo_meta_description ' ,
284287 'seo_url_key '
285288 ];
286- $ articles = Cache::remember ($ cacheKey , $ expiresAt , function () use ($ validated , $ fields ) {
289+ $ articles = Cache::remember (self :: CACHE_KEY_ARTICLE_RECOMMEND , $ expiresAt , function () use ($ validated , $ fields ) {
287290 return BlogArticle::query ()
288291 ->where ('status ' , 1 ) // 只推荐已发布文章
289292 ->when ($ validated ['seo_url_key ' ] ?? false , function ($ q ) use ($ validated ) {
@@ -642,6 +645,8 @@ public function batchUpdateArticleStatus(Request $request)
642645 'updated_at ' => now ()
643646 ]);
644647
648+ Cache::tags ([self ::CACHE_KEY_ARTICLE_RECOMMEND ])->flush ();
649+
645650 return response ()->json ([
646651 'success ' => true ,
647652 'message ' => "成功更新 {$ affected } 篇文章状态 " ,
@@ -685,6 +690,8 @@ public function batchDeleteArticle(Request $request)
685690 // 执行删除
686691 BlogArticle::whereIn ('id ' , $ ids )->delete ();
687692
693+ Cache::tags ([self ::CACHE_KEY_ARTICLE_RECOMMEND ])->flush ();
694+
688695 return response ()->json ([
689696 'success ' => true ,
690697 'message ' => '删除成功 ' ,
0 commit comments