diff --git a/app/Http/Controllers/Api/MediaController.php b/app/Http/Controllers/Api/MediaController.php index 65049b7..c0213e3 100644 --- a/app/Http/Controllers/Api/MediaController.php +++ b/app/Http/Controllers/Api/MediaController.php @@ -56,7 +56,7 @@ class MediaController extends Controller DB::statement("SET SQL_MODE=''"); /** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Media> */ - $mediaList = Media::withTrashed()->fromSub( + $mediaList = Media::cacheForever()->withTrashed()->fromSub( Media::query()->whereHas('articles', function ($q) use ($article) { $q->where('noArt', $article->noArt); })->whereHas('typeMedia', function ($q) { @@ -68,7 +68,7 @@ class MediaController extends Controller ->limit(\PHP_INT_MAX), 'mediaList' )->groupBy(['mediaList.type_vue_id', 'mediaList.variante_id']) - ->paginate(); + ->paginate(); return new MediaListResource($mediaList); } diff --git a/app/Models/Article.php b/app/Models/Article.php index 1f2b609..676284b 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -3,9 +3,67 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Rennokki\QueryCache\Traits\QueryCacheable; class Article extends Model { + use QueryCacheable; + + /** + * The timeout cache for this model + * + * @var integer + */ + protected $cacheFor = -1; + + /** + * The cache driver to be used. + * + * @var string + */ + public $cacheDriver = 'redis'; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted() + { + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass + static::created(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + }); + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass + static::updated(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + }); + // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass + static::deleted(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + }); + } + + // * Laravel Behaviors + + /** + * Set the base cache tags that will be present + * on all queries. + * + * ! This is used to flush caching + * + * @return array + */ + protected function getCacheBaseTags(): array + { + return [ + 'dam_article', + ]; + } + // * RELATIONSHIPS /** diff --git a/app/Models/Media.php b/app/Models/Media.php index a18a21d..41e49eb 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -16,13 +16,29 @@ use Illuminate\Database\Eloquent\Prunable; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\ValidationException; +use Rennokki\QueryCache\Traits\QueryCacheable; class Media extends Model { use BindedToElasticSearch; use Prunable; + use QueryCacheable; use SoftDeletes; + /** + * The timeout cache for this model + * + * @var integer + */ + protected $cacheFor = -1; + + /** + * The cache driver to be used. + * + * @var string + */ + public $cacheDriver = 'redis'; + /** * The relations to eager load on every query. * @@ -186,6 +202,9 @@ class Media extends Model protected static function booted() { static::created(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + /** @var \App\Models\User */ $user = auth()->user(); MediaCreated::dispatch($user, $model); @@ -201,6 +220,9 @@ class Media extends Model } }); static::updated(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + /** @var \App\Models\User */ $user = auth()->user(); @@ -221,6 +243,9 @@ class Media extends Model $model->addToElasticSearch(); }); static::deleted(function (self $model) { + // * Flush Eloquent cache. + self::flushQueryCache(); + /** @var \App\Models\User */ $user = auth()->user(); MediaDestroyed::dispatch($user, $model); @@ -234,6 +259,21 @@ class Media extends Model // * Laravel Behaviors + /** + * Set the base cache tags that will be present + * on all queries. + * + * ! This is used to flush caching + * + * @return array + */ + protected function getCacheBaseTags(): array + { + return [ + 'dam_media', + ]; + } + /** * Get the prunable model query. * diff --git a/composer.lock b/composer.lock index e5b409d..1e5225a 100644 --- a/composer.lock +++ b/composer.lock @@ -9786,5 +9786,5 @@ "platform-overrides": { "php": "8.1" }, - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.3.0" }