在Laravel中使用缓存分页

在Laravel中使用缓存分页,laravel,laravel-5.3,Laravel,Laravel 5.3,有人能帮我吗?我使用redis缓存。但当我使用分页时,我在每个页面上都看到相同的结果。我怎样才能修好它?谢谢。您应该缓存每页的结果,并使用当前页面的键 $currentPage = request()->get('page',1); $category = Cache::remember('sellcategory-' . $currentPage, 10, function(){ return DB::table('elans')->orderBy('updated_at

有人能帮我吗?我使用redis缓存。但当我使用分页时,我在每个页面上都看到相同的结果。我怎样才能修好它?谢谢。

您应该缓存每页的结果,并使用当前页面的键

$currentPage = request()->get('page',1);

$category = Cache::remember('sellcategory-' . $currentPage, 10, function(){
    return DB::table('elans')->orderBy('updated_at', 'desc')->where(['derc' => 1,'elaninnovu' => 'Satılır'])->paginate(10);
});

此解决方案用于缓存和清除分页缓存

如何缓存:

$page = request()->get('page', 1);
$limit = request()->get('limit', 10);

$users = Cache::remember('admin' . $page, 10, function() use ($limit){
    return DB::table('users')->paginate($limit);
});
您可以使用循环检查前缀,然后像这样删除它们

public static function forgetCaches($prefix)
{
    // Increase loop if you need, the loop will stop when key not found
    for ($i=1; $i < 1000; $i++) {
        $key = $prefix . $i;
        if (Cache::has($key)) {
            Cache::forget($key);
        } else {
            break;
        }
    }
}

如果没有看到问题的一些代码/屏幕截图,可能无法添加一些代码?-这有帮助吗?这是一个代码示例,如何在没有缓存标记的情况下删除(忘记/刷新)新添加记录上的这些键(因为“文件”缓存驱动程序),请?谢谢。@JakubAdamec,如果你不喜欢的话;如果不想使用Redis缓存驱动程序来处理标记,可以循环记录的页数,只需删除所有键。
// Clear caches:
forgetCaches('admin');