diff --git a/src/CachesValue.php b/src/CachesValue.php index 3dc6a54..b72eccd 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -7,6 +7,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Str; use ReflectionClass; use Vormkracht10\PermanentCache\Events\PermanentCacheUpdated; use Vormkracht10\PermanentCache\Events\PermanentCacheUpdating; @@ -43,13 +44,6 @@ trait CachesValue */ protected $expression = null; - /** - * The parameters this cache should be stored with. - * - * @var array - */ - protected $parameters = []; - /** * Indicates whether this cache is currently updating or not. * @@ -75,12 +69,8 @@ final public function handle($event = null): void ? Blade::renderComponent($this) : $this->run($event); - $expression = (new ReflectionClass(static::class))->getProperty('expression')->getDefaultValue(); - Cache::driver($driver)->forever($cacheKey, (object) [ 'value' => $value, - 'size' => strlen($value), - 'expression' => $expression, 'updated_at' => now(), ]); @@ -105,13 +95,7 @@ public function getParameters() */ private static function store($parameters): array { - $class = static::class; - - $store = (new ReflectionClass($class)) - ->getProperty('store') - ->getDefaultValue(); - - return self::parseCacheString($class, $store, $parameters); + return self::getCacheKey($parameters); } public function isCached($parameters = []): bool @@ -119,9 +103,8 @@ public function isCached($parameters = []): bool [$driver, $cacheKey] = self::store($parameters ?? []); $cache = Cache::driver($driver); - $cached = $cache->get($cacheKey); - return $cached && filled($cached?->value); + return $cache->has($cacheKey); } /** @@ -255,9 +238,17 @@ private static function getUpdateMethodString(): string /** * @return array{string, string} */ - private static function parseCacheString($class, ?string $store, ?array $parameters = []): array + public static function getCacheKey(?array $parameters = [], ?string $store = null, ?string $class = null): array { - if ($store && strpos($store, ':')) { + $class ??= static::class; + $store ??= (new ReflectionClass($class)) + ->getProperty('store') + ->getDefaultValue(); + + if ( + ! is_null($store) && + strpos($store, ':') + ) { $cacheDriver = substr($store, 0, strpos($store, ':')); $cacheKey = substr($store, strpos($store, ':') + 1); } else { @@ -265,7 +256,7 @@ private static function parseCacheString($class, ?string $store, ?array $paramet } $cacheDriver ??= config('cache.default'); - $cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(\Str::snake($class))); + $cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(Str::snake($class))); if ($parameters) { $cacheKey .= ':'.http_build_query($parameters); diff --git a/src/Commands/PermanentCachesStatusCommand.php b/src/Commands/PermanentCachesStatusCommand.php index 2125bdb..04531c6 100644 --- a/src/Commands/PermanentCachesStatusCommand.php +++ b/src/Commands/PermanentCachesStatusCommand.php @@ -3,6 +3,9 @@ namespace Vormkracht10\PermanentCache\Commands; use Illuminate\Console\Command; +use Illuminate\Console\Scheduling\Schedule; +use Lorisleiva\CronTranslator\CronTranslator; +use ReflectionClass; use Spatie\Emoji\Emoji; use Symfony\Component\Console\Helper\TableSeparator; use Vormkracht10\PermanentCache\Facades\PermanentCache; @@ -30,6 +33,11 @@ public function handle() { $caches = PermanentCache::configuredCaches(); + $frequencies = collect(app(Schedule::class)->events()) + ->mapWithKeys(function ($schedule) { + return [$schedule->description => CronTranslator::translate($schedule->expression)]; + }); + foreach ($caches as $c) { $cache = $caches->current(); $parameters = $caches->getInfo(); @@ -46,9 +54,9 @@ public function handle() $row = [ $cache->isCached($parameters) ? Emoji::checkMarkButton() : Emoji::crossMark(), $cache->getName(), - is_object($cached) ? readable_size($cached->size) : 'N/A', - is_object($cached) ? $cached->expression : 'N/A', - is_object($cached) ? $cached->updated_at->diffForHumans() : 'N/A', + $cached ? readable_size(strlen(serialize($cached))) : 'N/A', + $cached?->updated_at?->diffForHumans() ?: 'N/A', + $frequencies[$cache->getName()] ?? 'N/A', ]; if ($this->option('parameters')) { @@ -62,7 +70,7 @@ public function handle() array_pop($cachesTable); $this->table( - [null, 'Cache', 'Size', 'Frequency', 'Last Updated'] + ($this->option('parameters') ? ['Parameters'] : []), + [null, 'Cache', 'Size', 'Last Updated', 'Frequency'] + ($this->option('parameters') ? ['Parameters'] : []), $cachesTable, 'box', ); diff --git a/src/Commands/UpdatePermanentCachesCommand.php b/src/Commands/UpdatePermanentCachesCommand.php index 172ff8b..f9b7770 100644 --- a/src/Commands/UpdatePermanentCachesCommand.php +++ b/src/Commands/UpdatePermanentCachesCommand.php @@ -3,7 +3,6 @@ namespace Vormkracht10\PermanentCache\Commands; use Illuminate\Console\Command; -use ReflectionClass; use Spatie\Emoji\Emoji; use SplObjectStorage; use Symfony\Component\Console\Helper\ProgressBar; @@ -52,7 +51,6 @@ public function handle() $progressBar = $this->output->createProgressBar($caches->count()); $progressBar->setFormat('custom'); - $progressBar->setMessage('Starting...'); $progressBar->start();