Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Cache/Paginate Issue

So I decided to go from Laravel 4 to 5 which took me around 1-2 days because I barely knew how to do the transition. While doing the Upgrade for my app i came across a small problem with Json Pagination.

This code is what allows a PageQuery to be Paginated Via KnockoutJS

/**
 * 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);
}

which eventually lead to this error in this screenshot TaggedFile Cache Error

The Error directs to the code above and this code specifically

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

Im using a custom Cache Driver called TaggedFile. This worked fine in L4 but came across errors because There were some files removed within the Cache Alias. Like the StoreInterface. Can I receive some help for this? If you need me to post anything I will.

More Stuff:

Before I used this to Register the taggedFile Driver in global.php:

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

I do not know where exactly to put this. Does anyone know the equivalent of that? I tried putting it in AppServiceProvider but an error came up saying:

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

This used to work in L4 so i decided to go into the vendor folder manually find what the problem was....

This only had: getFacadeAccessor (Which L4 also only had but extend worked) So i decided to use getFacadeAccessor and it worked, but i don't know if that was the solution or not.

like image 888
Terrabyte Avatar asked May 01 '15 07:05

Terrabyte


1 Answers

As you noticed you are passing an array as a $key value, the safest way would be to replace the code

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

With

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

NB: I am not sure what version of php you are running, but json_encode( ... ) is normally faster then serialize( ... )

like image 166
Simon Goodman Avatar answered Nov 13 '22 19:11

Simon Goodman