gpt4 book ai didi

php - Laravel 5 缓存/分页问题

转载 作者:可可西里 更新时间:2023-11-01 13:01:09 26 4
gpt4 key购买 nike

所以我决定从 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 中注册标记文件驱动程序之前:

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 中工作,所以我决定手动进入 vendor 文件夹查找问题所在....

这只有:getFacadeAccessor(L4 也只有但扩展有效)所以我决定使用 getFacadeAccessor 并且它有效,但我不知道这是否是解决方案。

最佳答案

正如您注意到的那样,您将数组作为 $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( ... ) 快

关于php - Laravel 5 缓存/分页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com