Skip to content

Instantly share code, notes, and snippets.

@saniyathossain
Last active January 1, 2020 09:18
Show Gist options
  • Save saniyathossain/89dd3ce1c82df9d7d7f5eb11bac96897 to your computer and use it in GitHub Desktop.
Save saniyathossain/89dd3ce1c82df9d7d7f5eb11bac96897 to your computer and use it in GitHub Desktop.
Laravel cache code snippet
<?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