Last active
January 1, 2020 09:18
-
-
Save saniyathossain/89dd3ce1c82df9d7d7f5eb11bac96897 to your computer and use it in GitHub Desktop.
Laravel cache code snippet
This file contains 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
<?php | |
namespace App\Http\Models; | |
use App\Http\Base\Models\BaseModel; | |
class Student extends BaseModel | |
{ | |
/** | |
* getCacheData | |
* | |
* This is a sample method for store, get, reset cache data both for db query or api call response data | |
* | |
* @param string $cacheKey | |
* @param int $cacheExpiryTime | |
* @param bool $cacheReset | |
* | |
* @return mixed | |
*/ | |
public function getCacheData(string $cacheKey, int $cacheExpiryTime, bool $cacheReset = false) | |
{ | |
if ($cacheReset): | |
cache()->forget($cacheKey); | |
endif; | |
return cache()->remember($cacheKey, $cacheExpiryTime, function() { | |
// using cache for query | |
// return $this->pluck($this->qualifyColumn('name'), $this->getQualifiedKeyName())->toArray(); | |
// or use api response data | |
$client = new \GuzzleHttp\Client; | |
return $client->request('GET', '/endpoint')->getBody(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment