Created
February 5, 2018 10:09
-
-
Save bitclaw/ccce1696c2ae4ab5e12b6565c69f8236 to your computer and use it in GitHub Desktop.
Fast idea for cache pagination with Laravel 5.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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