亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 緩存與路由模型綁定?

Laravel 緩存與路由模型綁定?

PHP
ITMISS 2022-07-22 19:14:29
我正在向我的 Laravel 應用程序路由添加緩存。我有一個在我的網站上呈現博客文章的功能:    public function show(Post $post)    {        SEO::setTitle($post->title);        SEO::setDescription($post->subtitle);        SEO::setCanonical('https://employbl.com/blog/' . $post->slug);        SEO::opengraph()->setUrl('https://employbl.com/blog/' . $post->slug);        SEO::opengraph()->addProperty('type', 'article');        SEO::opengraph()->addImage($post->featured_image);        SEO::twitter()->setSite('@Employbl_Jobs');        $markdown = Markdown::parse($post->body);        return view('blog.post', compact('post', 'markdown'));    }這是調用該方法的路由:Route::get('/blog/{post}', 'PostController@show')->name('posts.show');以便我的博客呈現一個帶有 slug 的 URL,例如:https ://employbl.com/blog/laravel-vue-tailwindcss-single-page-application-spa在此路由上實現緩存以使用戶更快地加載頁面的最佳方法是什么?會不會是這樣的:$post = Cache::rememberForever('blog-post' . $post->id, function(){     return $post;});或者路由模型綁定是否需要緩存?緩存鍵是否需要唯一,或者我可以只使用“博客文章”作為緩存鍵嗎?$markdown緩存變量而不是變量會更好$post嗎?兩個都?
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

你在這里有幾個問題,所以我會盡力回答每個問題。答案可能不是完美的,因為我現在沒有任何方式參考或確認自己的記憶。


如果您嘗試緩存視圖的最終輸出,則可以有效地將最終視圖調用替換為:


return Cache::rememberForever('blog-post' . $post->id, function() use ($post) {


    // Do your SEO and markdown stuff here


    return view('blog.post', compact('post', 'markdown'))->render();

});

緩存鍵對于帖子必須是唯一的。模型路由系統對緩存系統一無所知,它只是將值傳遞給控制器的一種方式,控制器根據 URI 對傳入數據進行一些假設。所以你現在做的很好。


你的問題是我應該緩存帖子、降價還是兩者兼而有之?是它可能不會有所作為


1)您正在調用模型 GET 路由。這具有每次從數據庫加載 Post 的效果,使 Post 本身的緩存無關緊要。即使渲染視圖本身的緩存也是如此。


2) 您的視圖調用需要 Post 本身作為參數 [of compact() ]。您需要從某個地方加載它,這意味著再次調用數據庫來檢索帖子。


3)您正在使用Cache::rememberForever這意味著緩存永遠不會過期。所以在第一次之后加載 Post 是沒有意義的,因為它永遠不會被再次使用(結果會被永久緩存?。?。除非您使緩存無效(這使rememberForever變得毫無意義) ,否則未來的編輯(如果有的話)將不起作用。


因此,對于這種情況,我建議您遠離模型路線,而是嘗試使用傳統的基于 id 的路線


public function show(Request $request, $id)

{

    return Cache::remember('blog-post'.$id, ttl, function() use($id) {

      $post = Post::find($id);

      // Do SEO and markdown stuff

      return view('blog.post', compact('post', 'markdown'))->render();

    });

}

其中ttl是緩存過期的時間。


查看完整回答
反對 回復 2022-07-22
?
米琪卡哇伊

TA貢獻1998條經驗 獲得超6個贊

我正在尋求解決使用路由模型綁定綁定的緩存模型的類似問題,并找到了以下解決方案。


// On the Model class add the following method.


public function resolveRouteBinding($value, $field = null): ?Model

{

    return Cache::remember('my.custom.key'.$value, 3600, function () use ($value) {

        return $this->where('slug', $value)->firstOrFail();

    });

}

可以在此處找到方法詳細信息:自定義解析邏輯


值得注意的是,您很有可能寧愿在沒有該Cache::remember()方法的情況下使用它,這樣您就不會緩存返回 null 的內容。最好通過以下方式執行此操作:


// On the Model class add the following method.


public function resolveRouteBinding($value, $field = null): ?Model

{

    $cacheName = "my.custom.key.{$value}";


    if (Cache::has($cacheName)) {

        return Cache::get($cacheName);

    }


    $result = $this->query('slug', $value)->firstOrFail();

    Cache::put($cacheName, $result, 3600);


    return $result;

}


查看完整回答
反對 回復 2022-07-22
  • 2 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號