2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Traits\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-14 16:06:42 +01:00
|
|
|
* ! Use this only on limited elements (not on tags, products, or posts, for example).
|
2026-06-14 21:32:07 +02:00
|
|
|
*
|
|
|
|
|
* @template TModel of \Illuminate\Database\Eloquent\Model
|
|
|
|
|
*
|
2026-11-14 16:06:42 +01:00
|
|
|
* @phpstan-require-extends \Illuminate\Database\Eloquent\Model
|
2026-06-14 21:32:07 +02:00
|
|
|
*
|
|
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model
|
|
|
|
|
*/
|
|
|
|
|
trait IsCachedAsCollection
|
|
|
|
|
{
|
|
|
|
|
public static function bootIsCachedAsCollection(): void
|
|
|
|
|
{
|
2026-11-14 16:06:42 +01:00
|
|
|
$flush = static fn () => static::removeCached();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
static::saved($flush);
|
|
|
|
|
static::deleted($flush);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-14 16:06:42 +01:00
|
|
|
* @return Collection<int, TModel>
|
2026-06-14 21:32:07 +02:00
|
|
|
*/
|
|
|
|
|
public static function getCached(): Collection
|
|
|
|
|
{
|
2026-11-14 16:06:42 +01:00
|
|
|
return Cache::remember(
|
|
|
|
|
static::getCacheKeyName(),
|
|
|
|
|
48 * 60 * 60,
|
|
|
|
|
function () {
|
|
|
|
|
$query = static::query();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-14 16:06:42 +01:00
|
|
|
$cacheConditions = static::getCacheConditions();
|
|
|
|
|
|
|
|
|
|
if ($cacheConditions !== []) {
|
|
|
|
|
$query->where($cacheConditions);
|
|
|
|
|
}
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-14 16:06:42 +01:00
|
|
|
$results = static::queryCached($query)->get();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-14 16:06:42 +01:00
|
|
|
return static::filterQuery($results);
|
|
|
|
|
}
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function removeCached(): void
|
|
|
|
|
{
|
|
|
|
|
Cache::forget(static::getCacheKeyName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-14 16:06:42 +01:00
|
|
|
* @return array<string, string|int|float|bool>
|
|
|
|
|
*/
|
|
|
|
|
protected static function getCacheConditions(): array
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Builder<TModel> $query
|
|
|
|
|
* @return Builder<TModel>
|
2026-06-14 21:32:07 +02:00
|
|
|
*/
|
|
|
|
|
protected static function queryCached(Builder $query): Builder
|
|
|
|
|
{
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-14 16:06:42 +01:00
|
|
|
* @param Collection<int, TModel> $results
|
|
|
|
|
* @return Collection<int, TModel>
|
2026-06-14 21:32:07 +02:00
|
|
|
*/
|
|
|
|
|
protected static function filterQuery(Collection $results): Collection
|
|
|
|
|
{
|
|
|
|
|
return $results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected static function getCacheKeyName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'model_cache_' . strtolower(class_basename(static::class));
|
|
|
|
|
}
|
|
|
|
|
}
|