Laravel中缓存的分页问题:在每个页面上看到相同的结果
我正在使用缓存,但是当我使用分页时,我在每个页面上看到相同的结果。我怎样才能修好它 对于创建、更新、编辑和保存,我使用Laravel Model Observer 我的后置控制器Laravel中缓存的分页问题:在每个页面上看到相同的结果,laravel,caching,pagination,Laravel,Caching,Pagination,我正在使用缓存,但是当我使用分页时,我在每个页面上看到相同的结果。我怎样才能修好它 对于创建、更新、编辑和保存,我使用Laravel Model Observer 我的后置控制器 public function allpost(Request $request) { if($request->cache =='flush') { Cache::flush(); } $data =[];
public function allpost(Request $request)
{
if($request->cache =='flush')
{
Cache::flush();
}
$data =[];
$posts =Cache::get('posts',[]);
if(empty($posts))
{
$posts = Post::paginate(10);
Cache::forever('posts',$posts);
}
$data['posts'] = $posts ;
return view('frontend.allpost',$data);
}
<?php
namespace App\Observers;
use Illuminate\Support\Facades\Cache;
class PostObserver
{
public function created()
{
Cache::forget('posts');
}
public function updated()
{
Cache::forget('posts');
}
public function saved()
{
Cache::forget('posts');
}
public function deleted()
{
Cache::forget('posts');
}
}
My ModelObserver
public function allpost(Request $request)
{
if($request->cache =='flush')
{
Cache::flush();
}
$data =[];
$posts =Cache::get('posts',[]);
if(empty($posts))
{
$posts = Post::paginate(10);
Cache::forever('posts',$posts);
}
$data['posts'] = $posts ;
return view('frontend.allpost',$data);
}
<?php
namespace App\Observers;
use Illuminate\Support\Facades\Cache;
class PostObserver
{
public function created()
{
Cache::forget('posts');
}
public function updated()
{
Cache::forget('posts');
}
public function saved()
{
Cache::forget('posts');
}
public function deleted()
{
Cache::forget('posts');
}
}
当然可以,您只需将前10篇文章分页即可。我认为您需要缓存所有帖子,然后对结果进行分页。如何对每页进行分页以进行缓存?您可以chunk
这些帖子,并获得与页面匹配的分块帖子部分。请参阅此处的拉维收藏