This package allows to have static local cache on your models.
It can both be used with a cache server or without.
You can install the package via composer:
composer require vemcogroup/model-cache
It's easy to use the trait in your own class just Use the trait as below
use Vemcogroup\ModelCache\Traits\HaslocalCache;
class DataRepository
{
use HasLocalCache;
public function getData($userId)
{
$localCacheKey = 'getData.'.$userId;
/*
* If localCache exists with key return cache
*/
if ($cache = self::getLocalCache($localCacheKey)) {
return $cache;
}
// Run DB query or Cache code
$result = [1, 2, 3, 4, 5, 6];
/*
* Remember to save your DB query or Cache result
*/
self::setLocalCache($localCacheKey, $result);
return $result;
}
}
If you need to clear the Cache you can use the clear method
self::clearLocalCache();