diff --git a/src/CachesValue.php b/src/CachesValue.php index 80e9659..bffd6a6 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -2,6 +2,7 @@ namespace Vormkracht10\PermanentCache; +use Cron\CronExpression; use Illuminate\Bus\Queueable; use Illuminate\Console\Scheduling\CallbackEvent; use Illuminate\Support\Arr; @@ -166,7 +167,7 @@ final public static function get($parameters = [], $default = null, bool $update return static::updateAndGet($parameters ?? []); } - return $cache->get($cacheKey, $default)?->value; + return $cache->get($cacheKey)?->value ?? $default; } final public function getMeta($parameters = []): mixed @@ -195,9 +196,7 @@ final protected function value($default = null): mixed [$store, $cacheKey] = $this->store($this->getParameters()); - return Cache::store($store)->get( - $cacheKey, $default, - )?->value; + return Cache::store($store)->get($cacheKey)?->value ?? $default; } public function getName(): string @@ -309,4 +308,13 @@ public function addMarkers($value): mixed return $this->getMarker().$value.$this->getMarker(close: true); } + + public function expression(): ?CronExpression + { + if (! isset($this->expression)) { + return null; + } + + return new CronExpression($this->expression); + } } diff --git a/src/Commands/PermanentCachesStatusCommand.php b/src/Commands/PermanentCachesStatusCommand.php index 09d4d7e..9463a90 100644 --- a/src/Commands/PermanentCachesStatusCommand.php +++ b/src/Commands/PermanentCachesStatusCommand.php @@ -4,10 +4,13 @@ use Illuminate\Console\Command; use Illuminate\Console\Scheduling\Schedule; +use Illuminate\Support\Collection; use Lorisleiva\CronTranslator\CronTranslator; use Spatie\Emoji\Emoji; use Symfony\Component\Console\Helper\TableSeparator; +use Vormkracht10\PermanentCache\CachesValue; use Vormkracht10\PermanentCache\Facades\PermanentCache; +use Vormkracht10\PermanentCache\Scheduled; class PermanentCachesStatusCommand extends Command { @@ -16,7 +19,11 @@ class PermanentCachesStatusCommand extends Command * * @var string */ - protected $signature = 'permanent-cache:status {--P|parameters} {--F|filter=}'; + protected $signature = 'permanent-cache:status + {--P|parameters} + {--F|filter=} + {--S|sort= : The statistic to sort on, this can be one of ["size", "updated", "frequency"]} + {--A|ascending : Whether the sorting should be done ascending instead of descending}'; /** * The console command description. @@ -25,12 +32,65 @@ class PermanentCachesStatusCommand extends Command */ protected $description = 'Show status for all registered Permanent Caches'; + protected function getSize($cache): int + { + return strlen(serialize($cache)); + } + + protected function sortOnSize($a, $b): int + { + $sizeA = $this->getSize($a[0]); + $sizeB = $this->getSize($b[0]); + + return match (true) { + $sizeA == $sizeB => 0, + default => $sizeA > $sizeB ? 1 : -1, + }; + } + + protected function sortOnUpdated($a, $b): int + { + return $a[2]->updated_at > $b[2]->updated_at ? 1 : -1; + } + + protected function sortOnFrequency($a, $b): int + { + $a = $a[0]->expression()?->getNextRunDate(); + $b = $b[0]->expression()?->getNextRunDate(); + + if (is_null($a)) return is_null($b) ? 0 : -1; + if (is_null($b)) return 1; + + return $a > $b ? 1 : -1; + } + /** * Execute the console command. */ public function handle() { - $caches = PermanentCache::configuredCaches(); + $sortOn = $this->option('sort'); + + $items = new Collection; + + foreach (($caches = PermanentCache::configuredCaches()) as $cache) { + $items->push([ + $cache, + $parameters = $caches->getInfo(), + $cache->getMeta($parameters), + ]); + } + + $caches = $items->when($sortOn, fn ($collection) => $collection->sort(function ($a, $b) use ($sortOn) { + $result = match ($sortOn) { + 'size' => $this->sortOnSize($a, $b), + 'updated' => $this->sortOnUpdated($a, $b), + 'frequency' => $this->sortOnFrequency($a, $b), + default => throw new \Exception("Invalid sorting method: \"{$sortOn}\"") + }; + + return $this->option('ascending') ? $result : -$result; + })); $frequencies = collect(app(Schedule::class)->events()) ->mapWithKeys(function ($schedule) { @@ -39,10 +99,7 @@ public function handle() $tableRows = []; - foreach ($caches as $c) { - $cache = $caches->current(); - $parameters = $caches->getInfo(); - + foreach ($caches as [$cache, $parameters, $meta]) { if ( $this->option('filter') && ! str_contains(strtolower($cache->getName()), strtolower($this->option('filter'))) @@ -50,13 +107,11 @@ public function handle() continue; } - $cached = $cache->getMeta($parameters); - $row = [ $cache->isCached($parameters) ? Emoji::checkMarkButton() : Emoji::crossMark(), $cache->getName(), - $cached ? readable_size(strlen(serialize($cached))) : 'N/A', - $cached?->updated_at?->diffForHumans() ?: 'N/A', + $meta ? readable_size($this->getSize($meta)) : 'N/A', + $meta?->updated_at?->diffForHumans() ?: 'N/A', $frequencies[$cache->getName()] ?? 'N/A', ];