Skip to content

Commit

Permalink
fix: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
efureev committed Mar 4, 2022
1 parent 7f5b24c commit 500e3d1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Traits/Models/HasModelEntityCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ protected static function cacheForgetFn(): callable

protected static function cacheForget(Model $model): bool
{
if (!static::$cacheEnable) {
return true;
}

return Cache::forget(
static::cachePrefixKey($model->{static::cacheKeyName()})
);
Expand Down Expand Up @@ -79,7 +83,6 @@ protected static function cacheKeyName(): string
return 'id';
}


protected static function cachePrefixKey(string $key = null, string $prefix = null): string
{
$prefix ??= class_basename(static::class);
Expand All @@ -92,8 +95,14 @@ protected static function cacheTtl(): int
return 60 * 60;
}

public static bool $cacheEnable = true;

public static function remember(callable $fn, string $key): mixed
{
if (!static::$cacheEnable) {
return $fn();
}

return Cache::remember(
static::cachePrefixKey($key),
static::cacheTtl(),
Expand Down

0 comments on commit 500e3d1

Please sign in to comment.