laravel - Laravel 中缓存的分页问题:在每个页面上看到相同的结果
问题描述
我正在使用缓存,但是当我使用分页时,我在每个页面上看到相同的结果。我该如何解决?
对于创建、更新、编辑、保存,我使用 Laravel Model Observer。
我的邮政控制器
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');
}
}
解决方案
推荐阅读
- laravel - 如何将应用的路由中间件名称获取到 AppServiceProvider 中?
- android - 如何通过单击通知从当前 Activity 调用方法
- apache-kafka - 无法为 kafka 设置 connection.url 为 oracle 设置连接设置
- powershell - 将 Invoke-Command 脚本块内部的变量发送回主机
- java - 运行 Solr "4.10.2" 需要 Java 1.7 或更高版本,但安装了 java 版本 "14.0.1"
- javascript - 如何为 alexa 技能创建共享槽
- python - 从旧文件读取数据,将其写入新文件,然后在 Python 中删除旧文件时出错
- reactjs - 递归渲染反应后获取元素大小
- java - protoc 自定义插件出错,程序未找到或不可执行
- css - 如何在之前和之后为元素设置伪 CSS