From 500e3d1ff7e0dcbf67f7dd170f3e9836f9b828ce Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 4 Mar 2022 14:39:27 +0500 Subject: [PATCH] fix: cache --- src/Traits/Models/HasModelEntityCache.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Traits/Models/HasModelEntityCache.php b/src/Traits/Models/HasModelEntityCache.php index d83ded4..9d053db 100644 --- a/src/Traits/Models/HasModelEntityCache.php +++ b/src/Traits/Models/HasModelEntityCache.php @@ -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()}) ); @@ -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); @@ -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(),