Skip to content

Commit 393047e

Browse files
committed
refactor: improve cache component dx
1 parent c1561e0 commit 393047e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1325
-549
lines changed

packages/cache/src/Cache.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,40 @@
77
use Closure;
88
use Psr\Cache\CacheItemInterface;
99
use Tempest\DateTime\DateTimeInterface;
10+
use Tempest\DateTime\Duration;
1011

1112
interface Cache
1213
{
13-
public function put(string $key, mixed $value, ?DateTimeInterface $expiresAt = null): CacheItemInterface;
14-
14+
/**
15+
* Whether the cache is enabled.
16+
*/
17+
public bool $enabled {
18+
get;
19+
set;
20+
}
21+
22+
/**
23+
* Sets the specified key to the specified value in the cache. Optionally, specify an expiration.
24+
*/
25+
public function put(string $key, mixed $value, null|Duration|DateTimeInterface $expiration = null): CacheItemInterface;
26+
27+
/**
28+
* Gets the value associated with the specified key from the cache. If the key does not exist, null is returned.
29+
*/
1530
public function get(string $key): mixed;
1631

17-
/** @param Closure(): mixed $cache */
18-
public function resolve(string $key, Closure $cache, ?DateTimeInterface $expiresAt = null): mixed;
32+
/**
33+
* If the specified key already exists in the cache, the value is returned and the `$callback` is not executed. Otherwise, the result of the callback is stored, then returned.
34+
*/
35+
public function resolve(string $key, Closure $callback, null|Duration|DateTimeInterface $expiration = null): mixed;
1936

37+
/**
38+
* Removes the specified key from the cache.
39+
*/
2040
public function remove(string $key): void;
2141

42+
/**
43+
* Clears the entire cache.
44+
*/
2245
public function clear(): void;
23-
24-
public function isEnabled(): bool;
2546
}

packages/cache/src/CacheClearCommand.php

Lines changed: 0 additions & 52 deletions
This file was deleted.

packages/cache/src/CacheConfig.php

Lines changed: 0 additions & 98 deletions
This file was deleted.

packages/cache/src/CacheDiscovery.php

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Tempest\Cache;
4+
5+
interface CacheException
6+
{
7+
}

packages/cache/src/CacheInitializer.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,46 @@
44

55
namespace Tempest\Cache;
66

7+
use Tempest\Cache\Config\CacheConfig;
78
use Tempest\Container\Container;
8-
use Tempest\Container\Initializer;
9+
use Tempest\Container\DynamicInitializer;
910
use Tempest\Container\Singleton;
11+
use Tempest\Reflection\ClassReflector;
1012

11-
final readonly class CacheInitializer implements Initializer
13+
use function Tempest\env;
14+
use function Tempest\Support\str;
15+
16+
final readonly class CacheInitializer implements DynamicInitializer
1217
{
18+
public function canInitialize(ClassReflector $class, ?string $tag): bool
19+
{
20+
return $class->getType()->matches(Cache::class);
21+
}
22+
1323
#[Singleton]
14-
public function initialize(Container $container): Cache|ProjectCache
24+
public function initialize(ClassReflector $class, ?string $tag, Container $container): Cache
25+
{
26+
return new GenericCache(
27+
cacheConfig: $container->get(CacheConfig::class, $tag),
28+
enabled: $this->shouldCacheBeEnabled($tag),
29+
);
30+
}
31+
32+
private function shouldCacheBeEnabled(?string $tag): bool
1533
{
16-
return new ProjectCache($container->get(CacheConfig::class));
34+
$globalCacheEnabled = (bool) env('CACHE_ENABLED', default: true);
35+
36+
if (! $tag) {
37+
return $globalCacheEnabled;
38+
}
39+
40+
$environmentVariableName = str($tag)
41+
->snake()
42+
->upper()
43+
->prepend('CACHE_')
44+
->append('_ENABLED')
45+
->toString();
46+
47+
return (bool) env($environmentVariableName, default: $globalCacheEnabled);
1748
}
1849
}

packages/cache/src/CacheStatusCommand.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)