static::removeCached(); static::saved($flush); static::deleted($flush); } /** * @return Collection */ public static function getCached(): Collection { return Cache::remember( static::getCacheKeyName(), 48 * 60 * 60, function () { $query = static::query(); $cacheConditions = static::getCacheConditions(); if ($cacheConditions !== []) { $query->where($cacheConditions); } $results = static::queryCached($query)->get(); return static::filterQuery($results); } ); } public static function removeCached(): void { Cache::forget(static::getCacheKeyName()); } /** * @return array */ protected static function getCacheConditions(): array { return []; } /** * @param Builder $query * @return Builder */ protected static function queryCached(Builder $query): Builder { return $query; } /** * @param Collection $results * @return Collection */ protected static function filterQuery(Collection $results): Collection { return $results; } protected static function getCacheKeyName(): string { return 'model_cache_' . strtolower(class_basename(static::class)); } }