redis caching for used models on api

This commit is contained in:
Jérémy Munsch 2022-08-01 16:27:22 +02:00
parent b06642b1bf
commit d5edbebbdf
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
4 changed files with 101 additions and 3 deletions

View file

@ -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);
}

View file

@ -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
/**

View file

@ -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.
*

2
composer.lock generated
View file

@ -9786,5 +9786,5 @@
"platform-overrides": {
"php": "8.1"
},
"plugin-api-version": "2.2.0"
"plugin-api-version": "2.3.0"
}