Skip to content

Instantly share code, notes, and snippets.

@bitclaw
Created February 5, 2018 10:09
Show Gist options
  • Save bitclaw/ccce1696c2ae4ab5e12b6565c69f8236 to your computer and use it in GitHub Desktop.
Save bitclaw/ccce1696c2ae4ab5e12b6565c69f8236 to your computer and use it in GitHub Desktop.
Fast idea for cache pagination with Laravel 5.1
/**
* Cache delete handler for Laravel 5.1 pagination.
*
* @array list of laravel cache keys
* @param int $totalResults Total results for pagination
* @param int $perPage Page results
* @return Response
*
* You need to cache your pages with this key pattern:
* Cache::rememberForever('key.' . $currentPage , function(){});
* Default $currentPage must be 1
* if(!$currentPage)$currentPage = 1
*/
public function clearPaginateCache(array $array, $totalResults, $perPage)
{
$totalPages = ceil($totalResults/$perPage) + 1;
foreach ($array as $key => $value)
{
for($x = 1; $x <= $totalPages; $x++)
{
Cache::forget($value . "." . $x);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment