Skip to content

Commit 65a1c01

Browse files
xHeaveninnocenzi
andauthored
refactor(cache): several fixes and improvements for the cache package (#1760)
Co-authored-by: Enzo Innocenzi <[email protected]>
1 parent e324f6e commit 65a1c01

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

packages/cache/src/Commands/CacheStatusCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
public function __invoke(bool $internal = true): void
3939
{
4040
if (! $this->container instanceof GenericContainer) {
41-
$this->console->error('Clearing caches is only available when using the default container.');
41+
$this->console->error('Checking cache status is only available when using the default container.');
4242
return;
4343
}
4444

packages/cache/src/GenericCache.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public function get(Stringable|string $key): mixed
130130

131131
public function getMany(iterable $key): array
132132
{
133+
if (! $this->enabled) {
134+
return Arr\map_with_keys(
135+
array: $key,
136+
map: fn (string|Stringable $key) => yield (string) $key => null,
137+
);
138+
}
139+
133140
return Arr\map_with_keys(
134141
array: $key,
135142
map: fn (string|Stringable $key) => yield (string) $key => $this->adapter->getItem((string) $key)->get(),

packages/cache/src/InternalCacheInsightsProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Tempest\Core\ConfigCache;
66
use Tempest\Core\DiscoveryCache;
7+
use Tempest\Core\DiscoveryCacheStrategy;
78
use Tempest\Core\Insight;
89
use Tempest\Core\InsightsProvider;
910
use Tempest\Icon\IconCache;
@@ -26,7 +27,11 @@ public function getInsights(): array
2627
'Discovery' => match ($this->discoveryCache->valid) {
2728
false => new Insight('Invalid', Insight::ERROR),
2829
true => match ($this->discoveryCache->enabled) {
29-
true => new Insight('Enabled', Insight::ERROR),
30+
true => match ($this->discoveryCache->strategy) {
31+
DiscoveryCacheStrategy::FULL => new Insight('Enabled', Insight::SUCCESS),
32+
DiscoveryCacheStrategy::PARTIAL => new Insight('Enabled (partial)', Insight::SUCCESS),
33+
default => null, // INVALID and NONE are handled
34+
},
3035
false => new Insight('Disabled', Insight::WARNING),
3136
},
3237
},

packages/cache/src/Testing/RestrictedCache.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,73 @@
1010
use Tempest\Cache\Lock;
1111
use Tempest\DateTime\DateTimeInterface;
1212
use Tempest\DateTime\Duration;
13+
use UnitEnum;
1314

1415
final class RestrictedCache implements Cache
1516
{
1617
public bool $enabled;
1718

1819
public function __construct(
19-
private ?string $tag = null,
20+
private null|string|UnitEnum $tag = null,
2021
) {}
2122

23+
private function resolveTag(): ?string
24+
{
25+
return $this->tag instanceof UnitEnum ? $this->tag->name : $this->tag;
26+
}
27+
2228
public function lock(Stringable|string $key, null|Duration|DateTimeInterface $expiration = null, null|Stringable|string $owner = null): Lock
2329
{
24-
throw new CacheUsageWasForbidden($this->tag);
30+
throw new CacheUsageWasForbidden($this->resolveTag());
2531
}
2632

2733
public function has(Stringable|string $key): bool
2834
{
29-
throw new CacheUsageWasForbidden($this->tag);
35+
throw new CacheUsageWasForbidden($this->resolveTag());
3036
}
3137

3238
public function put(Stringable|string $key, mixed $value, null|Duration|DateTimeInterface $expiration = null): CacheItemInterface
3339
{
34-
throw new CacheUsageWasForbidden($this->tag);
40+
throw new CacheUsageWasForbidden($this->resolveTag());
3541
}
3642

3743
public function putMany(iterable $values, null|Duration|DateTimeInterface $expiration = null): array
3844
{
39-
throw new CacheUsageWasForbidden($this->tag);
45+
throw new CacheUsageWasForbidden($this->resolveTag());
4046
}
4147

4248
public function increment(Stringable|string $key, int $by = 1): int
4349
{
44-
throw new CacheUsageWasForbidden($this->tag);
50+
throw new CacheUsageWasForbidden($this->resolveTag());
4551
}
4652

4753
public function decrement(Stringable|string $key, int $by = 1): int
4854
{
49-
throw new CacheUsageWasForbidden($this->tag);
55+
throw new CacheUsageWasForbidden($this->resolveTag());
5056
}
5157

5258
public function get(Stringable|string $key): mixed
5359
{
54-
throw new CacheUsageWasForbidden($this->tag);
60+
throw new CacheUsageWasForbidden($this->resolveTag());
5561
}
5662

5763
public function getMany(iterable $key): array
5864
{
59-
throw new CacheUsageWasForbidden($this->tag);
65+
throw new CacheUsageWasForbidden($this->resolveTag());
6066
}
6167

6268
public function resolve(Stringable|string $key, Closure $callback, null|Duration|DateTimeInterface $expiration = null, ?Duration $stale = null): mixed
6369
{
64-
throw new CacheUsageWasForbidden($this->tag);
70+
throw new CacheUsageWasForbidden($this->resolveTag());
6571
}
6672

6773
public function remove(Stringable|string $key): void
6874
{
69-
throw new CacheUsageWasForbidden($this->tag);
75+
throw new CacheUsageWasForbidden($this->resolveTag());
7076
}
7177

7278
public function clear(): void
7379
{
74-
throw new CacheUsageWasForbidden($this->tag);
80+
throw new CacheUsageWasForbidden($this->resolveTag());
7581
}
7682
}

0 commit comments

Comments
 (0)