Skip to content

Commit c1635fe

Browse files
committed
feat: add redis cache and insights support
1 parent d1fc8d7 commit c1635fe

18 files changed

+206
-77
lines changed

packages/cache/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"symfony/cache": "^7.2",
88
"tempest/core": "dev-main",
99
"tempest/clock": "dev-main",
10+
"tempest/kv-store": "dev-main",
1011
"tempest/container": "dev-main"
1112
},
1213
"require-dev": {

packages/cache/src/CacheInitializer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ public function canInitialize(ClassReflector $class, null|string|UnitEnum $tag):
2525
#[Singleton]
2626
public function initialize(ClassReflector $class, null|string|UnitEnum $tag, Container $container): Cache
2727
{
28+
$config = $container->get(CacheConfig::class, $tag);
29+
2830
return new GenericCache(
29-
cacheConfig: $container->get(CacheConfig::class, $tag),
30-
deferredTasks: $container->get(DeferredTasks::class),
31+
adapter: $config->createAdapter($container),
3132
enabled: $this->shouldCacheBeEnabled($tag),
33+
deferredTasks: $container->get(DeferredTasks::class),
34+
tag: $tag,
3235
);
3336
}
3437

packages/cache/src/Commands/CacheStatusCommand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,10 @@ private function getCacheName(Cache $cache): string
9999
return $cache::class;
100100
}
101101

102-
$tag = $cache->cacheConfig->tag;
103-
104-
if ($tag instanceof UnitEnum) {
105-
return $tag->name;
102+
if ($cache->tag instanceof UnitEnum) {
103+
return $cache->tag->name;
106104
}
107105

108-
return $tag ?? 'default';
106+
return $cache->tag ?? 'default';
109107
}
110108
}

packages/cache/src/Config/CacheConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Tempest\Cache\Config;
44

55
use Psr\Cache\CacheItemPoolInterface;
6+
use Tempest\Container\Container;
67
use Tempest\Container\HasTag;
78

89
interface CacheConfig extends HasTag
910
{
1011
/**
1112
* Creates the adapter.
1213
*/
13-
public function createAdapter(): CacheItemPoolInterface;
14+
public function createAdapter(Container $container): CacheItemPoolInterface;
1415
}

packages/cache/src/Config/CustomCacheConfig.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tempest\Cache\Config;
44

55
use Symfony\Component\Cache\Adapter\AdapterInterface;
6+
use Tempest\Container\Container;
67
use UnitEnum;
78

89
use function Tempest\get;
@@ -26,8 +27,8 @@ public function __construct(
2627
public null|string|UnitEnum $tag = null,
2728
) {}
2829

29-
public function createAdapter(): AdapterInterface
30+
public function createAdapter(Container $container): AdapterInterface
3031
{
31-
return get($this->adapter);
32+
return $container->get($this->adapter);
3233
}
3334
}

packages/cache/src/Config/FilesystemCacheConfig.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tempest\Cache\Config;
44

55
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
6+
use Tempest\Container\Container;
67
use UnitEnum;
78

89
use function Tempest\internal_storage_path;
@@ -29,7 +30,7 @@ public function __construct(
2930
public null|string|UnitEnum $tag = null,
3031
) {}
3132

32-
public function createAdapter(): FilesystemAdapter
33+
public function createAdapter(Container $container): FilesystemAdapter
3334
{
3435
return new FilesystemAdapter(
3536
namespace: $this->namespace ?? '',

packages/cache/src/Config/InMemoryCacheConfig.php

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

55
use Symfony\Component\Cache\Adapter\ArrayAdapter;
66
use Tempest\Clock\Clock;
7+
use Tempest\Container\Container;
78
use UnitEnum;
89

9-
use function Tempest\get;
10-
1110
/**
1211
* Store cache in an array, for a single request.
1312
*/
@@ -20,10 +19,10 @@ public function __construct(
2019
public null|string|UnitEnum $tag = null,
2120
) {}
2221

23-
public function createAdapter(): ArrayAdapter
22+
public function createAdapter(Container $container): ArrayAdapter
2423
{
2524
return new ArrayAdapter(
26-
clock: get(Clock::class)->toPsrClock(),
25+
clock: $container->get(Clock::class)->toPsrClock(),
2726
);
2827
}
2928
}

packages/cache/src/Config/PhpCacheConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Tempest\Cache\Config;
44

5-
use Psr\Cache\CacheItemPoolInterface;
65
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
6+
use Tempest\Container\Container;
77
use UnitEnum;
88

99
use function Tempest\internal_storage_path;
@@ -30,7 +30,7 @@ public function __construct(
3030
public null|string|UnitEnum $tag = null,
3131
) {}
3232

33-
public function createAdapter(): PhpFilesAdapter
33+
public function createAdapter(Container $container): PhpFilesAdapter
3434
{
3535
return new PhpFilesAdapter(
3636
namespace: $this->namespace ?? '',
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tempest\Cache\Config;
4+
5+
use Symfony\Component\Cache\Adapter\RedisAdapter;
6+
use Tempest\Container\Container;
7+
use Tempest\KeyValue\Redis\Redis;
8+
use UnitEnum;
9+
10+
/**
11+
* Use a Redis connection for caching.
12+
*/
13+
final class RedisCacheConfig implements CacheConfig
14+
{
15+
public function __construct(
16+
/**
17+
* The directory where the cache files are stored.
18+
*/
19+
public ?string $directory = null,
20+
21+
/**
22+
* Optional namespace to avoid collisions with other caches in the same directory.
23+
*/
24+
public ?string $namespace = null,
25+
26+
/*
27+
* Identifies the {@see \Tempest\Cache\Cache} instance in the container, in case you need more than one configuration.
28+
*/
29+
public null|string|UnitEnum $tag = null,
30+
) {}
31+
32+
public function createAdapter(Container $container): RedisAdapter
33+
{
34+
return new RedisAdapter(
35+
redis: $container->get(Redis::class)->getClient(),
36+
namespace: $this->namespace ?? '',
37+
);
38+
}
39+
}

packages/cache/src/GenericCache.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66
use Psr\Cache\CacheItemInterface;
77
use Psr\Cache\CacheItemPoolInterface;
88
use Stringable;
9-
use Tempest\Cache\Config\CacheConfig;
109
use Tempest\Core\DeferredTasks;
1110
use Tempest\DateTime\DateTime;
1211
use Tempest\DateTime\DateTimeInterface;
1312
use Tempest\DateTime\Duration;
1413
use Tempest\Support\Arr;
1514
use Tempest\Support\Random;
15+
use UnitEnum;
1616

1717
final class GenericCache implements Cache
1818
{
1919
public function __construct(
20-
private(set) CacheConfig $cacheConfig,
20+
private(set) CacheItemPoolInterface $adapter,
2121
public bool $enabled = true,
22-
private ?CacheItemPoolInterface $adapter = null,
2322
private ?DeferredTasks $deferredTasks = null,
24-
) {
25-
$this->adapter ??= $this->cacheConfig->createAdapter();
26-
}
23+
public null|UnitEnum|string $tag = null,
24+
) {}
2725

2826
public function lock(Stringable|string $key, null|Duration|DateTimeInterface $expiration = null, null|Stringable|string $owner = null): Lock
2927
{

0 commit comments

Comments
 (0)