Я пытаюсь кэшировать определенные модели (я использую драйвер файла кэша) в моем проекте.Когда я загружаю Model :: all (), панель отладки laravel показывает 4 запроса (модель и атрибут protected $with
).Я делаю это, используя черту, которую я назвал Cacheable.
Черта:
namespace App\Cache\Traits;
use Illuminate\Database\Eloquent\Model;
use Cache;
trait Cacheable {
public static function bootCacheable() {
static::saved(function (Model $model) {
Cache::forever(md5(get_class($model).$model->id)), $model);
});
static::deleted(function (Model $model)
{
Cache::forget(md5(get_class($model).$model->id));
});
static::updated(function (Model $model) {
Cache::forever(md5(get_class($model).$model->id), $model);
});
static::retrieved(function (Model $model) {
Cache::forever(md5(get_class($model).$model->id), $model);
});
}
}
Модель:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Classes\ChallengeStatus;
use App\Cache\Traits\Cacheable;
class Challenge extends Model
{
use Cacheable;
protected $with = ['platform', 'brand', 'challengeType'];
public function platform() {
return $this->belongsTo('App\Models\Platform');
}
public function brand() {
return $this->belongsTo('App\Models\Brand');
}
public function challengeType() {
return $this->belongsTo('App\Models\ChallengeType');
}
public function reports() {
return $this->hasMany('App\Models\Report');
}
}
Чего мне не хватает в этом подходе?Я не могу понять это.Заранее спасибо за помощь.