Laravel 缓存与路由模型绑定?

我正在向我的 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吗?两个都?


ITMISS
浏览 158回答 2
2回答

白衣染霜花

你在这里有几个问题,所以我会尽力回答每个问题。答案可能不是完美的,因为我现在没有任何方式参考或确认自己的记忆。如果您尝试缓存视图的最终输出,则可以有效地将最终视图调用替换为: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是缓存过期的时间。

米琪卡哇伊

我正在寻求解决使用路由模型绑定绑定的缓存模型的类似问题,并找到了以下解决方案。// 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;}
打开App,查看更多内容
随时随地看视频慕课网APP