/**
* Класс подписывается на события создания, обновления и удаления экземпляра модели, и удаляет соответсвующий кэш
*
* Class CacheableObserver
* @package app\Observers
*/
class CacheableObserver
{
/**
* The Cache repository implementation
* @var Repository
*/
private $cache;
/**
* CacheableObserver constructor.
* @param $cache
*/
public function __construct(Repository $cache)
{
$this->cache = $cache;
}
public function created($model)
{
if ($model instanceof Cacheable) {
$this->clearCache($model);
}
}
public function updating($model)
{
if ($model instanceof Cacheable) {
$this->clearCache($model);
}
}
public function deleting($model)
{
if ($model instanceof Cacheable) {
$this->clearCache($model);
}
}
/**
* Clear cache of given model
* @param $model
*/
private function clearCache($model)
{
$this->cache
->tags($model->cacheTags)
->flush();
}
}