Skip to content

Commit bba425c

Browse files
authored
Merge pull request #17 from vormkracht10/feature/sort-in-status-command
add sorting to the status command
2 parents 034d895 + 89ff1c9 commit bba425c

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

src/CachesValue.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Vormkracht10\PermanentCache;
44

5+
use Cron\CronExpression;
56
use Illuminate\Bus\Queueable;
67
use Illuminate\Console\Scheduling\CallbackEvent;
78
use Illuminate\Support\Arr;
@@ -166,7 +167,7 @@ final public static function get($parameters = [], $default = null, bool $update
166167
return static::updateAndGet($parameters ?? []);
167168
}
168169

169-
return $cache->get($cacheKey, $default)?->value;
170+
return $cache->get($cacheKey)?->value ?? $default;
170171
}
171172

172173
final public function getMeta($parameters = []): mixed
@@ -195,9 +196,7 @@ final protected function value($default = null): mixed
195196

196197
[$store, $cacheKey] = $this->store($this->getParameters());
197198

198-
return Cache::store($store)->get(
199-
$cacheKey, $default,
200-
)?->value;
199+
return Cache::store($store)->get($cacheKey)?->value ?? $default;
201200
}
202201

203202
public function getName(): string
@@ -309,4 +308,13 @@ public function addMarkers($value): mixed
309308

310309
return $this->getMarker().$value.$this->getMarker(close: true);
311310
}
311+
312+
public function expression(): ?CronExpression
313+
{
314+
if (! isset($this->expression)) {
315+
return null;
316+
}
317+
318+
return new CronExpression($this->expression);
319+
}
312320
}

src/Commands/PermanentCachesStatusCommand.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Console\Scheduling\Schedule;
7+
use Illuminate\Support\Collection;
78
use Lorisleiva\CronTranslator\CronTranslator;
89
use Spatie\Emoji\Emoji;
910
use Symfony\Component\Console\Helper\TableSeparator;
11+
use Vormkracht10\PermanentCache\CachesValue;
1012
use Vormkracht10\PermanentCache\Facades\PermanentCache;
13+
use Vormkracht10\PermanentCache\Scheduled;
1114

1215
class PermanentCachesStatusCommand extends Command
1316
{
@@ -16,7 +19,11 @@ class PermanentCachesStatusCommand extends Command
1619
*
1720
* @var string
1821
*/
19-
protected $signature = 'permanent-cache:status {--P|parameters} {--F|filter=}';
22+
protected $signature = 'permanent-cache:status
23+
{--P|parameters}
24+
{--F|filter=}
25+
{--S|sort= : The statistic to sort on, this can be one of ["size", "updated", "frequency"]}
26+
{--A|ascending : Whether the sorting should be done ascending instead of descending}';
2027

2128
/**
2229
* The console command description.
@@ -25,12 +32,65 @@ class PermanentCachesStatusCommand extends Command
2532
*/
2633
protected $description = 'Show status for all registered Permanent Caches';
2734

35+
protected function getSize($cache): int
36+
{
37+
return strlen(serialize($cache));
38+
}
39+
40+
protected function sortOnSize($a, $b): int
41+
{
42+
$sizeA = $this->getSize($a[0]);
43+
$sizeB = $this->getSize($b[0]);
44+
45+
return match (true) {
46+
$sizeA == $sizeB => 0,
47+
default => $sizeA > $sizeB ? 1 : -1,
48+
};
49+
}
50+
51+
protected function sortOnUpdated($a, $b): int
52+
{
53+
return $a[2]->updated_at > $b[2]->updated_at ? 1 : -1;
54+
}
55+
56+
protected function sortOnFrequency($a, $b): int
57+
{
58+
$a = $a[0]->expression()?->getNextRunDate();
59+
$b = $b[0]->expression()?->getNextRunDate();
60+
61+
if (is_null($a)) return is_null($b) ? 0 : -1;
62+
if (is_null($b)) return 1;
63+
64+
return $a > $b ? 1 : -1;
65+
}
66+
2867
/**
2968
* Execute the console command.
3069
*/
3170
public function handle()
3271
{
33-
$caches = PermanentCache::configuredCaches();
72+
$sortOn = $this->option('sort');
73+
74+
$items = new Collection;
75+
76+
foreach (($caches = PermanentCache::configuredCaches()) as $cache) {
77+
$items->push([
78+
$cache,
79+
$parameters = $caches->getInfo(),
80+
$cache->getMeta($parameters),
81+
]);
82+
}
83+
84+
$caches = $items->when($sortOn, fn ($collection) => $collection->sort(function ($a, $b) use ($sortOn) {
85+
$result = match ($sortOn) {
86+
'size' => $this->sortOnSize($a, $b),
87+
'updated' => $this->sortOnUpdated($a, $b),
88+
'frequency' => $this->sortOnFrequency($a, $b),
89+
default => throw new \Exception("Invalid sorting method: \"{$sortOn}\"")
90+
};
91+
92+
return $this->option('ascending') ? $result : -$result;
93+
}));
3494

3595
$frequencies = collect(app(Schedule::class)->events())
3696
->mapWithKeys(function ($schedule) {
@@ -39,24 +99,19 @@ public function handle()
3999

40100
$tableRows = [];
41101

42-
foreach ($caches as $c) {
43-
$cache = $caches->current();
44-
$parameters = $caches->getInfo();
45-
102+
foreach ($caches as [$cache, $parameters, $meta]) {
46103
if (
47104
$this->option('filter') &&
48105
! str_contains(strtolower($cache->getName()), strtolower($this->option('filter')))
49106
) {
50107
continue;
51108
}
52109

53-
$cached = $cache->getMeta($parameters);
54-
55110
$row = [
56111
$cache->isCached($parameters) ? Emoji::checkMarkButton() : Emoji::crossMark(),
57112
$cache->getName(),
58-
$cached ? readable_size(strlen(serialize($cached))) : 'N/A',
59-
$cached?->updated_at?->diffForHumans() ?: 'N/A',
113+
$meta ? readable_size($this->getSize($meta)) : 'N/A',
114+
$meta?->updated_at?->diffForHumans() ?: 'N/A',
60115
$frequencies[$cache->getName()] ?? 'N/A',
61116
];
62117

0 commit comments

Comments
 (0)