我正在向我的 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吗?两个都?
白衣染霜花
米琪卡哇伊