Laravel cache::remember 正在将对象作为数组返回

Laravel cache::remember is returing object as an array

Laravel Cache::remember 正在 return 将 LengthAwarePaginator 对象作为数组。

function getNotifications( $userID ) {
    $values = Cache::remember('cache-key', 10, function() {

        $query = DB::table( 'user_notifications' )
                ->leftJoin( 'notifications', 'user_notifications.notification_id', '=', 'notifications.id' )
                ->where( 'user_notifications.user_id', $userID )
                ->select( 'notifications.*' )
                ->orderBy('created_at', 'DESC')
                ->paginate(5);

        return $query;

    });

    return $values;
}

如果我在 return 从缓存关闭之前 dd($query),它是 return 以下对象,它接受 $value->links() 来显示分页。

但是每当缓存将 $query 存储到 $values 中时,它会将值 return 作为一个数组:

我试着注释掉反序列化块:

/*foreach( $values as $key => $value ) :
    $values[$key]->meta = self::maybeUnserialize($value->meta);
endforeach;*/

并确认,这不是原因。

我也试过,但是失败了:

$values = collect($values);

通过多次检查和交叉检查,我确认,问题是 Cache::remember

如何强制 Cache::remember return 保持原样?这样我就可以让 $object->links() 为我工作。

实际代码可以找到here

问题是,缓存是用来存储数据的,而不是实例。所以有两种方法可以做到这一点:

  1. 缓存时,缓存信息每页,或
  2. 获取所有数据,但自己分页

解决方案 1:

我们去了第二个。但是如果你需要第一个解决方案,这里是代码,我得到了 from Laracasts,由 chrisml 提供:

$statuses = Cache::remember("statuses_{$id}_page_{$page}", 3, function() use ($event, $sort) {
    return $event->statuses()
        ->with('comments')
        ->latest()
        ->paginate(10);
});

在上面的代码中,缓存键在每一页上都在变化,所以缓存是按页存储的。

解决方案 2:

但就我而言,我们认为我们应该选择第二个,就我们而言,这对我们来说是明智的。因此,我们必须制作自己的分页。幸运的是 psampaz 在他们的博客上为我们做了基础:

因此,我们不是使用 ->paginate(),而是先获取所有数据,然后像以前一样缓存它们。

$values = Cache::remember('cache-key', 10, function() {

    $query = DB::table( 'user_notifications' )
            ->leftJoin( 'notifications', 'user_notifications.notification_id', '=', 'notifications.id' )
            ->where( 'user_notifications.user_id', $userID )
            ->select( 'notifications.*' )
            ->orderBy('created_at', 'DESC')
            ->get(); // <----------- here

    return $query;

});

但在返回 $values 之前,我们正在制作自己的分页。我们对 psampaz 的代码进行了一些修复:

use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

function getNotifications( $userID ) {

    // Collapsed the cached values here :)
    $values = Cache::remember('cache-key', 10, function() {...});

    // Get current page form url e.g. &page=6.
    $currentPage = LengthAwarePaginator::resolveCurrentPage();

    // Get current path.
    $currentPath = LengthAwarePaginator::resolveCurrentPath();

    // Create a new Laravel collection from the array data.
    $collection = new Collection($values);

    // Define how many items we want to be visible on each page.
    $perPage = 5;

    // Slice the collection to get the items to display on the current page.
    $results = $collection->slice(($currentPage - 1) * $perPage, $perPage)->all();

    // Create our paginator and pass it to the view.
    $values = new LengthAwarePaginator($results, count($collection), $perPage, $currentPage, ['path' => $currentPath]);

    return $values;
}

最后,我们可以轻松地使用 $object->links() 进行分页,这太棒了! :)