Laravel 5缓存/分页问题

11

所以我决定将 Laravel 4 升级到 5,整个过程花费了我大约 1-2 天时间,因为我几乎不知道如何进行转换。在升级我的应用程序时,我遇到了一个小问题,涉及到 Json 分页。

以下是可以通过 KnockoutJS 进行分页的 PageQuery 代码:

/**
 * Builds paginate query with given parameters.
 * 
 * @param  array   $params
 * @param  integer $page
 * @param  integer $perPage
 * 
 * @return array
 */
public function buildPaginateQuery(array $params, $page = 1, $perPage = 15)
{
    $query = $this->model;

    $query = $this->appendParams($params, $query);

    $count = (new Cache)->remember('count', '2000', function() use ($query){
        return $query->count();
    });

    $totalPages = $count / $perPage;

    $query = $query->skip($perPage * ($page - 1))->take($perPage);

    $query = $query->order(isset($params['order']) && $params['order'] ? $params['order'] : null);

    //$query = $query->cacheTags(array($this->model->table, 'pagination'))->remember(2000);

    $query = (new Cache)->remember(array($this->model->table, 'pagination'), '2000', function() use ($query){
        return $query;
    });

    return array('query' => $query, 'totalPages' => $totalPages, 'totalItems' => $count);
}

这最终导致了屏幕截图中显示的错误: TaggedFile Cache Error

该错误指向上面的代码,具体指向以下代码:

/**
 * Get the full path for the given cache key.
 *
 * @param  string  $key
 * @return string
 */
protected function path($key)
{
    $parts = array_slice(str_split($hash = md5($key), 2), 0, 2);
    $path  = $this->directory() . '/'.join('/', $parts).'/'.$hash;

    //unset the tags so we use the base cache folder if no
    //tags are passed with subsequent call to the same instance
    //of this class
    //$this->tags = array();

    return $path;
}

我正在使用一个名为TaggedFile的自定义缓存驱动程序。在L4中,这个驱动程序可以正常工作,但是因为缓存别名中有一些文件被删除了,所以遇到了错误,例如StoreInterface。请问我能够得到一些关于此问题的帮助吗?如果需要我发布任何内容,请告诉我。

更多内容:

之前,我用以下代码在global.php中注册taggedFile驱动程序:

Cache::extend('taggedFile', function($app)
{
    return new Illuminate\Cache\Repository(new Lib\Extensions\TaggedFileCache);
});

我不知道应该把这个放在哪里,有人知道相当于这个的东西是什么吗?我试着把它放在AppServiceProvider中,但是出现了一个错误,提示:

Call to undefined method Illuminate\Support\Facades\Cache::extend()

之前在L4(Laravel 4的缩写)中可以运行,所以我决定手动进入vendor文件夹查找问题所在...

这里只有:getFacadeAccessor(L4也只有这个,但extend函数可以正常工作) 所以我决定使用getFacadeAccessor,它起作用了,但我不知道是否是解决方案。

1个回答

1

正如您所注意到的,您正在将数组作为 $key 值传递,最安全的方式是替换代码

$parts = array_slice(str_split($hash = md5($key), 2), 0, 2);

带有

$parts = array_slice(str_split($hash = md5(json_encode($key)), 2), 0, 2);

注意:我不确定您正在运行的 php 版本,但是 json_encode( ... ) 通常比 serialize( ... ) 更快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接