diff --git a/app/Models/Prize.php b/app/Models/Prize.php index 8965904..c432264 100644 --- a/app/Models/Prize.php +++ b/app/Models/Prize.php @@ -3,6 +3,8 @@ namespace App\Models; use App\Lib\Helpers\FileStorageHelper; +use App\Traits\Models\IsCachedAsCollection; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; /** @@ -28,6 +30,8 @@ use Illuminate\Database\Eloquent\Model; */ class Prize extends Model { + use IsCachedAsCollection; + /** * The attributes that are mass assignable. * @@ -55,13 +59,18 @@ class Prize extends Model static::creating(function (self $model) { self::manageFields($model); }); + static::created(function () { + self::removeCached(); + }); static::updating(function (self $model) { self::manageFields($model); }); static::updated(function (self $model) { + self::removeCached(); FileStorageHelper::removeOldFile($model, 'pdf_winning_proof'); }); static::deleted(function (self $model) { + self::removeCached(); FileStorageHelper::removeFile($model, 'pdf_winning_proof'); }); } @@ -98,4 +107,17 @@ class Prize extends Model { return $this->hasMany(InstantWin::class, 'prize_id'); } + + // * CACHE METHOD + + /** + * Change query for cached elements + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + protected static function queryCached(Builder $query): Builder + { + return $query->with('instantWins'); + } } diff --git a/app/Models/Settings.php b/app/Models/Settings.php index ab53e90..52d74bc 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Traits\Models\IsCachedAsCollection; use Illuminate\Database\Eloquent\Model; /** @@ -33,6 +34,8 @@ use Illuminate\Database\Eloquent\Model; */ class Settings extends Model { + use IsCachedAsCollection; + /** * The attributes that are mass assignable. * @@ -72,4 +75,22 @@ class Settings extends Model 'teaser_end_activity' => 'datetime', 'close_page_active_date' => 'datetime', ]; + + /** + * Perform any actions required after the model boots. + * + * @return void + */ + protected static function booted() + { + static::created(function () { + self::removeCached(); + }); + static::updated(function () { + self::removeCached(); + }); + static::deleted(function () { + self::removeCached(); + }); + } } diff --git a/app/Providers/ViewServiceProvider.php b/app/Providers/ViewServiceProvider.php index 113b419..83ae955 100644 --- a/app/Providers/ViewServiceProvider.php +++ b/app/Providers/ViewServiceProvider.php @@ -44,24 +44,11 @@ class ViewServiceProvider extends ServiceProvider Paginator::useBootstrapFive(); } - // FIXME: USe trait cache here // On ramène le nom du jeu et le nom de la société sur toute les pages. - $settingsGlobal = Cache::get('settingsGlobal'); - if (!$settingsGlobal) { - $settingsGlobal = app(Settings::class) - ->select('name', 'company', 'analytics', 'start_date', 'end_date') - ->first(); - Cache::put('settingsGlobal', $settingsGlobal); - } - View::share('settingsGlobal', $settingsGlobal); + View::share('settingsGlobal', Settings::getCached()->first()); // On ramène aussi tout les prix car on en a besoin sur le header. - $prizesGlobal = Cache::get('prizesGlobal'); - if (!$prizesGlobal) { - $prizesGlobal = Prize::all(); - Cache::put('prizesGlobal', $prizesGlobal); - } - View::share('prizesGlobal', $prizesGlobal); + View::share('prizesGlobal', Prize::getCached()); // * CUSTOM IF STATEMENTS diff --git a/app/Traits/Models/IsCachedAsCollection.php b/app/Traits/Models/IsCachedAsCollection.php index f8ede85..87ed939 100644 --- a/app/Traits/Models/IsCachedAsCollection.php +++ b/app/Traits/Models/IsCachedAsCollection.php @@ -18,7 +18,7 @@ trait IsCachedAsCollection * Get all available instances from cache. * If cache was reset, it will fetch them from DB. * - * @return \Illuminate\Database\Eloquent\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public static function getCached(): Collection {