Skip to content

Commit ed638a6

Browse files
committed
refactor: update cache environment variables
1 parent 04687e6 commit ed638a6

File tree

11 files changed

+64
-80
lines changed

11 files changed

+64
-80
lines changed

.env.example

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,12 @@ ENVIRONMENT=local
44
# The base URI that's used for all generated URIs
55
BASE_URI=http://localhost
66

7-
# The CACHE key is used as a global override to turn all caches on or off
8-
# Should be true in production, but null or false in local development
9-
CACHE=null
7+
# Setting to `false` force-disable internal caches.
8+
INTERNAL_CACHES=true
109

11-
# Enable or disable discovery cache
10+
# Enable or disable discovery cache. Can be `true`, `partial` or `false`.
1211
DISCOVERY_CACHE=false
1312

14-
# Enable or disable config cache
15-
CONFIG_CACHE=false
16-
17-
# Enable or disable icon cache
18-
ICON_CACHE=true
19-
20-
# Enable or disable view cache
21-
VIEW_CACHE=false
22-
23-
# Enable or disable project cache (allround cache)
24-
PROJECT_CACHE=false
25-
2613
# Overwrite default log paths (null = default)
2714
DEBUG_LOG_PATH=null
28-
SERVER_LOG_PATH=null
15+
SERVER_LOG_PATH=null

packages/cache/src/Commands/CacheClearCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Tempest\Console\Middleware\CautionMiddleware;
1313
use Tempest\Container\Container;
1414
use Tempest\Container\GenericContainer;
15+
use Tempest\Core\ConfigCache;
1516
use Tempest\Core\DiscoveryCache;
1617
use Tempest\Support\Str;
1718
use Tempest\View\IconCache;
@@ -24,7 +25,7 @@
2425
{
2526
use HasConsole;
2627

27-
private const DEFAULT_CACHE = 'default';
28+
private const string DEFAULT_CACHE = 'default';
2829

2930
public function __construct(
3031
private Cache $cache,
@@ -54,7 +55,7 @@ public function __invoke(
5455

5556
private function clearInternalCaches(bool $all = false): void
5657
{
57-
$caches = [ViewCache::class, IconCache::class, DiscoveryCache::class];
58+
$caches = [ConfigCache::class, ViewCache::class, IconCache::class, DiscoveryCache::class];
5859

5960
if ($all === false && count($caches) > 1) {
6061
$caches = $this->ask(

packages/cache/src/Commands/CacheStatusCommand.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Tempest\Console\HasConsole;
1414
use Tempest\Container\Container;
1515
use Tempest\Container\GenericContainer;
16+
use Tempest\Core\AppConfig;
17+
use Tempest\Core\ConfigCache;
1618
use Tempest\Core\DiscoveryCache;
1719
use Tempest\Support\Str;
1820
use Tempest\View\IconCache;
@@ -28,6 +30,8 @@
2830
public function __construct(
2931
private Console $console,
3032
private Container $container,
33+
private AppConfig $appConfig,
34+
private DiscoveryCache $discoveryCache,
3135
) {}
3236

3337
#[ConsoleCommand(name: 'cache:status', description: 'Shows which caches are enabled')]
@@ -45,7 +49,7 @@ public function __invoke(bool $internal = true): void
4549
if ($internal) {
4650
$this->console->header('Internal caches');
4751

48-
foreach ([ViewCache::class, IconCache::class, DiscoveryCache::class] as $cacheName) {
52+
foreach ([ConfigCache::class, ViewCache::class, IconCache::class] as $cacheName) {
4953
/** @var Cache $cache */
5054
$cache = $this->container->get($cacheName);
5155

@@ -57,6 +61,22 @@ public function __invoke(bool $internal = true): void
5761
},
5862
);
5963
}
64+
65+
$this->console->keyValue(
66+
key: DiscoveryCache::class,
67+
value: match ($this->discoveryCache->valid) {
68+
false => '<style="bold fg-red">INVALID</style>',
69+
true => match ($this->discoveryCache->enabled) {
70+
true => '<style="bold fg-green">ENABLED</style>',
71+
false => '<style="bold fg-red">DISABLED</style>',
72+
},
73+
},
74+
);
75+
76+
if ($this->appConfig->environment->isProduction() && ! $this->discoveryCache->enabled) {
77+
$this->console->writeln();
78+
$this->console->error('Discovery cache is disabled in production. This is not recommended.');
79+
}
6080
}
6181

6282
$this->console->header('User caches');

packages/core/src/Commands/DiscoveryGenerateCommand.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\Console\HasConsole;
1010
use Tempest\Container\Container;
1111
use Tempest\Container\GenericContainer;
12+
use Tempest\Core\AppConfig;
1213
use Tempest\Core\DiscoveryCache;
1314
use Tempest\Core\DiscoveryCacheStrategy;
1415
use Tempest\Core\FrameworkKernel;
@@ -24,12 +25,13 @@
2425
public function __construct(
2526
private Kernel $kernel,
2627
private DiscoveryCache $discoveryCache,
28+
private AppConfig $appConfig,
2729
) {}
2830

2931
#[ConsoleCommand(name: 'discovery:generate', description: 'Compile and cache all discovery according to the configured discovery caching strategy')]
3032
public function __invoke(): void
3133
{
32-
$strategy = $this->resolveDiscoveryCacheStrategy();
34+
$strategy = DiscoveryCacheStrategy::make(env('DISCOVERY_CACHE', default: $this->appConfig->environment->isProduction()));
3335

3436
if ($strategy === DiscoveryCacheStrategy::NONE) {
3537
$this->info('Discovery cache disabled, nothing to generate.');
@@ -76,17 +78,6 @@ public function generateDiscoveryCache(DiscoveryCacheStrategy $strategy, Closure
7678
}
7779
}
7880

79-
private function resolveDiscoveryCacheStrategy(): DiscoveryCacheStrategy
80-
{
81-
$cache = env('CACHE');
82-
83-
if ($cache !== null) {
84-
return DiscoveryCacheStrategy::make($cache);
85-
}
86-
87-
return DiscoveryCacheStrategy::make(env('DISCOVERY_CACHE'));
88-
}
89-
9081
public function resolveKernel(): Kernel
9182
{
9283
$container = new GenericContainer();

packages/core/src/ConfigCacheInitializer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ final class ConfigCacheInitializer implements Initializer
1414
public function initialize(Container $container): ConfigCache
1515
{
1616
return new ConfigCache(
17-
enabled: $this->shouldCacheBeEnabled(),
17+
enabled: $this->shouldCacheBeEnabled(
18+
$container->get(AppConfig::class)->environment->isProduction(),
19+
),
1820
);
1921
}
2022

21-
private function shouldCacheBeEnabled(): bool
23+
private function shouldCacheBeEnabled(bool $isProduction): bool
2224
{
23-
if (env('CACHE') === true) {
24-
return true;
25+
if (env('INTERNAL_CACHES') === false) {
26+
return false;
2527
}
2628

27-
return (bool) env('CONFIG_CACHE', default: false);
29+
return (bool) env('CONFIG_CACHE', default: $isProduction);
2830
}
2931
}

packages/core/src/DiscoveryCacheInitializer.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,19 @@ final class DiscoveryCacheInitializer implements Initializer
1414
public function initialize(Container $container): DiscoveryCache
1515
{
1616
return new DiscoveryCache(
17-
strategy: $this->resolveDiscoveryCacheStrategy(),
17+
strategy: $this->resolveDiscoveryCacheStrategy(
18+
$container->get(AppConfig::class)->environment->isProduction(),
19+
),
1820
);
1921
}
2022

21-
private function resolveDiscoveryCacheStrategy(): DiscoveryCacheStrategy
23+
private function resolveDiscoveryCacheStrategy(bool $isProduction): DiscoveryCacheStrategy
2224
{
2325
if ($this->isDiscoveryGenerateCommand()) {
2426
return DiscoveryCacheStrategy::NONE;
2527
}
2628

27-
$cache = env('CACHE');
28-
29-
if ($cache !== null) {
30-
$current = DiscoveryCacheStrategy::make($cache);
31-
} else {
32-
$current = DiscoveryCacheStrategy::make(env('DISCOVERY_CACHE'));
33-
}
29+
$current = DiscoveryCacheStrategy::make(env('DISCOVERY_CACHE', default: $isProduction));
3430

3531
if ($current === DiscoveryCacheStrategy::NONE) {
3632
return $current;

packages/view/src/IconCacheInitializer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tempest\Container\Container;
66
use Tempest\Container\Initializer;
77
use Tempest\Container\Singleton;
8+
use Tempest\Core\AppConfig;
89

910
use function Tempest\env;
1011

@@ -14,16 +15,18 @@ final class IconCacheInitializer implements Initializer
1415
public function initialize(Container $container): IconCache
1516
{
1617
return new IconCache(
17-
enabled: $this->shouldCacheBeEnabled(),
18+
enabled: $this->shouldCacheBeEnabled(
19+
$container->get(AppConfig::class)->environment->isProduction(),
20+
),
1821
);
1922
}
2023

21-
private function shouldCacheBeEnabled(): bool
24+
private function shouldCacheBeEnabled(bool $isProduction): bool
2225
{
23-
if (env('CACHE') === true) {
24-
return true;
26+
if (env('INTERNAL_CACHES') === false) {
27+
return false;
2528
}
2629

27-
return (bool) env('ICON_CACHE', default: true);
30+
return (bool) env('ICON_CACHE', default: $isProduction);
2831
}
2932
}

packages/view/src/ViewCacheInitializer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tempest\Container\Container;
66
use Tempest\Container\Initializer;
77
use Tempest\Container\Singleton;
8+
use Tempest\Core\AppConfig;
89

910
use function Tempest\env;
1011

@@ -14,16 +15,18 @@ final class ViewCacheInitializer implements Initializer
1415
public function initialize(Container $container): ViewCache
1516
{
1617
return new ViewCache(
17-
enabled: $this->shouldCacheBeEnabled(),
18+
enabled: $this->shouldCacheBeEnabled(
19+
$container->get(AppConfig::class)->environment->isProduction(),
20+
),
1821
);
1922
}
2023

21-
private function shouldCacheBeEnabled(): bool
24+
private function shouldCacheBeEnabled(bool $isProduction): bool
2225
{
23-
if (env('CACHE') === true) {
24-
return true;
26+
if (env('INTERNAL_CACHES') === false) {
27+
return false;
2528
}
2629

27-
return (bool) env('VIEW_CACHE', default: false);
30+
return (bool) env('VIEW_CACHE', default: $isProduction);
2831
}
2932
}

tests/Integration/Cache/CacheClearCommandTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,4 @@ public function test_cache_clear_filter(): void
9292
->assertSee('my-cache')
9393
->assertSeeCount('CLEARED', expectedCount: 2);
9494
}
95-
96-
public function test_clear_internal_caches(): void
97-
{
98-
$this->console
99-
->call(CacheClearCommand::class, ['all' => true, 'internal' => true])
100-
->assertSee(ViewCache::class)
101-
->assertSee(IconCache::class)
102-
->assertSee(DiscoveryCache::class)
103-
->assertSeeCount('CLEARED', expectedCount: 3);
104-
}
105-
106-
public function test_clear_internal_cache(): void
107-
{
108-
$this->console
109-
->call(CacheClearCommand::class, ['internal' => true])
110-
->submit('0')
111-
->submit('yes')
112-
->assertSee(ViewCache::class)
113-
->assertSeeCount('CLEARED', expectedCount: 1);
114-
}
11595
}

tests/Integration/Cache/CacheStatusCommandTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public function test_with_internal_caches(): void
5151
->call(CacheStatusCommand::class, ['internal' => true])
5252
->assertSee(ViewCache::class)
5353
->assertSee(IconCache::class)
54-
->assertSee(DiscoveryCache::class)
55-
->assertSeeCount('DISABLED', expectedCount: 1)
56-
->assertSeeCount('ENABLED', expectedCount: 3);
54+
->assertSee(DiscoveryCache::class);
5755
}
5856
}

0 commit comments

Comments
 (0)