Merge branch 'addCache' into 'develop'
feat: ✨ add HasCache trait on models
See merge request ldc-web/projets/ldc!92
This commit is contained in:
commit
c1b8eae209
9 changed files with 38 additions and 54 deletions
|
|
@ -10,6 +10,7 @@ use App\Models\Document;
|
|||
use App\Models\DocumentCategory;
|
||||
use App\Models\DocumentTag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
use Uwil\LaravelBackend\Resources\CommonResource;
|
||||
|
|
@ -74,11 +75,11 @@ class DocumentController extends Controller
|
|||
*/
|
||||
public function create(Document $document): \Inertia\Response
|
||||
{
|
||||
$documentTags = DocumentTag::all();
|
||||
return Inertia::render('Document/Create', [
|
||||
'data' => CommonResource::make($document->load('tags')),
|
||||
'enumDocumentStatus' => DocumentStatusEnum::toArray(),
|
||||
'tags' => $documentTags,
|
||||
'tags' => Cache::get(DocumentTag::getCacheKey()),
|
||||
'categories' => Cache::get(DocumentCategory::getCacheKey()),
|
||||
'modelName' => $this->modelName,
|
||||
'enumYears' => YearsEnum::toArray()
|
||||
]);
|
||||
|
|
@ -134,8 +135,8 @@ class DocumentController extends Controller
|
|||
'data' => CommonResource::make($document->load('tags', 'categories')),
|
||||
'enumDocumentStatus' => DocumentStatusEnum::toArray(),
|
||||
'enumYears' => YearsEnum::toArray(),
|
||||
'tags' => $documentTags,
|
||||
'categories' => $documentCategories,
|
||||
'tags' => Cache::get(DocumentTag::getCacheKey()),
|
||||
'categories' => Cache::get(DocumentCategory::getCacheKey()),
|
||||
'modelName' => $this->modelName
|
||||
]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use App\Http\Requests\Back\News\UpdateNewsRequest;
|
|||
use App\Models\News;
|
||||
use App\Models\NewsCategory;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Inertia\Inertia;
|
||||
use Uwil\LaravelBackend\Resources\CommonResource;
|
||||
|
|
@ -81,7 +82,7 @@ class NewsController extends Controller
|
|||
'data' => CommonResource::make($news),
|
||||
'modelName' => $this->modelName,
|
||||
'enumNewsStatus' => NewsStatusEnum::toArray(),
|
||||
'newsCategory' => NewsCategory::all(),
|
||||
'newsCategory' => Cache::get(NewsCategory::getCacheKey()),
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
@ -132,7 +133,7 @@ class NewsController extends Controller
|
|||
'data' => CommonResource::make($news->load('categories')),
|
||||
'modelName' => $this->modelName,
|
||||
'enumNewsStatus' => NewsStatusEnum::toArray(),
|
||||
'newsCategory' => NewsCategory::all(),
|
||||
'newsCategory' => Cache::get(NewsCategory::getCacheKey())
|
||||
]);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use App\Contracts\TranslatableModel;
|
|||
use App\Enums\Document\DocumentStatusEnum;
|
||||
use App\Enums\YearsEnum;
|
||||
use App\Helpers\TranslationHelper;
|
||||
use App\Traits\Models\HasCache;
|
||||
use App\Traits\Models\HasTranslations;
|
||||
use App\Traits\Models\HasUwilImages;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
|
@ -13,7 +14,6 @@ use Illuminate\Database\Eloquent\Model;
|
|||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Support\Arr;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
||||
|
||||
class Document extends Model implements TranslatableModel
|
||||
|
|
@ -21,6 +21,7 @@ class Document extends Model implements TranslatableModel
|
|||
use ActivityLog;
|
||||
use HasFactory;
|
||||
use HasTranslations;
|
||||
use HasCache;
|
||||
use HasUwilImages {
|
||||
HasUwilImages::inertiaFill as traitInertiaFill;
|
||||
}
|
||||
|
|
@ -80,18 +81,9 @@ class Document extends Model implements TranslatableModel
|
|||
static::creating(function (self $model) {
|
||||
self::manageFields($model);
|
||||
});
|
||||
static::created(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
static::updating(function (self $model) {
|
||||
self::manageFields($model);
|
||||
});
|
||||
static::updated(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
static::deleted(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -180,33 +172,6 @@ class Document extends Model implements TranslatableModel
|
|||
self::makeStatusSense($model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all cache elements related to this model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function resetCache(): void
|
||||
{
|
||||
Cache::forget('getPublishedDocuments');
|
||||
self::getPublishedDocuments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all published news.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection<int,self>
|
||||
*/
|
||||
public static function getPublishedDocuments(): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('getPublishedDocuments', 86400, function () {
|
||||
return self::query()
|
||||
->where('status', DocumentStatusEnum::published)
|
||||
->with(['tags'])
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Assert fallback locale translation is present.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Models;
|
||||
|
||||
use App\Contracts\TranslatableModel;
|
||||
use App\Traits\Models\HasCache;
|
||||
use App\Traits\Models\HasTranslations;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -13,6 +14,7 @@ class DocumentCategory extends Model implements TranslatableModel
|
|||
{
|
||||
use ActivityLog;
|
||||
use HasFactory;
|
||||
use HasCache;
|
||||
use HasTranslations;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Models;
|
||||
|
||||
use App\Contracts\TranslatableModel;
|
||||
use App\Traits\Models\HasCache;
|
||||
use App\Traits\Models\HasTranslations;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -13,6 +14,7 @@ class DocumentTag extends Model implements TranslatableModel
|
|||
{
|
||||
use ActivityLog;
|
||||
use HasFactory;
|
||||
use HasCache;
|
||||
use HasTranslations;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ class News extends Model implements TranslatableModel
|
|||
use HasFactory;
|
||||
use HasTranslations;
|
||||
use SoftDeletes;
|
||||
use HasCache;
|
||||
use HasUwilImages {
|
||||
HasUwilImages::inertiaFill as traitInertiaFill;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\Models;
|
|||
|
||||
use App\Contracts\TranslatableModel;
|
||||
use App\Helpers\TranslationHelper;
|
||||
use App\Traits\Models\HasCache;
|
||||
use App\Traits\Models\HasTranslations;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
|
@ -24,6 +25,7 @@ class NewsCategory extends Model implements TranslatableModel
|
|||
|
||||
use ActivityLog;
|
||||
use HasFactory;
|
||||
use HasCache;
|
||||
use HasTranslations;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,6 +19,16 @@ trait HasCache
|
|||
*/
|
||||
public static function bootHasCache(): void
|
||||
{
|
||||
$hadSoftDelete = method_exists(static::class, 'initializeSoftDeletes');
|
||||
|
||||
if ($hadSoftDelete) {
|
||||
static::restored(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
static::forceDeleted(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
}
|
||||
static::created(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
|
|
@ -28,17 +38,11 @@ trait HasCache
|
|||
static::deleted(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
static::restored(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
static::forceDeleted(function () {
|
||||
self::resetCache();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all cache elements related to this model.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function resetCache(): void
|
||||
|
|
@ -73,7 +77,7 @@ trait HasCache
|
|||
|
||||
/**
|
||||
* Generate a unique cache key for the model.
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function getCacheKey(): string
|
||||
|
|
|
|||
|
|
@ -14,13 +14,16 @@ class DocumentCategorySeeder extends Seeder
|
|||
{
|
||||
app()->setLocale("fr");
|
||||
$docs = DocumentCategory::create([
|
||||
"name" => "Documents"
|
||||
"name" => "Documents" // pour finance
|
||||
]);
|
||||
$meetings = DocumentCategory::create([
|
||||
"name" => "Assemblées générales"
|
||||
"name" => "Assemblées générales" // pour finance
|
||||
]);
|
||||
$investors = DocumentCategory::create([
|
||||
"name" => "Présentation investisseurs"
|
||||
"name" => "Présentation investisseurs" // pour finance
|
||||
]);
|
||||
$actu = DocumentCategory::create([
|
||||
"name" => "Actualités"
|
||||
]);
|
||||
|
||||
app()->setLocale("en");
|
||||
|
|
@ -33,5 +36,8 @@ class DocumentCategorySeeder extends Seeder
|
|||
$investors->update([
|
||||
"name" => "Investor presentation"
|
||||
]);
|
||||
$actu->update([
|
||||
"name" => "News"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue