Caching Laravel cache::Memory正在将对象作为数组重新存储

Caching Laravel cache::Memory正在将对象作为数组重新存储,caching,laravel-5.2,Caching,Laravel 5.2,LaravelCache::Memory以数组形式返回LengthAwarePaginator对象 function getNotifications( $userID ) { $values = Cache::remember('cache-key', 10, function() { $query = DB::table( 'user_notifications' ) ->leftJoin( 'notifications', '

Laravel
Cache::Memory
以数组形式返回
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;
}
如果我在从缓存关闭返回之前
dd($query)
,它将返回以下对象,该对象接受
$value->links()
以显示分页。

但只要缓存将
$query
存储到
$values
中,它就会以数组的形式返回值:

我试着对unserialize块进行注释:

/*foreach( $values as $key => $value ) :
    $values[$key]->meta = self::maybeUnserialize($value->meta);
endforeach;*/
并证实,这不是原因

我也尝试过,但失败了:

$values = collect($values);
通过多次检查和交叉检查,我确认,问题在于
缓存::记住

如何强制
缓存::记住
按原样返回内容?这样我就可以让他为我工作


可以找到实际代码。

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

  • 缓存时,缓存每页的信息,或
  • 获取数据时获取所有数据,但进行自己的分页
  • 解决方案1: 我们第二次去了。但是,如果您需要使用第一种解决方案,以下是由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;
    
    });
    
    最后,我们可以轻松地使用
    $object->links()
    进行分页,这真是太棒了!)

    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;
    }