Skip to content

Commit 36edbd8

Browse files
innocenzibrendt
andauthored
feat(cache): separate internal and user caches (#1245)
Co-authored-by: Brent Roose <[email protected]>
1 parent b5420a9 commit 36edbd8

File tree

71 files changed

+2411
-602
lines changed

Some content is hidden

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

71 files changed

+2411
-602
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/Cache.php

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,88 @@
66

77
use Closure;
88
use Psr\Cache\CacheItemInterface;
9+
use Stringable;
910
use Tempest\DateTime\DateTimeInterface;
11+
use Tempest\DateTime\Duration;
1012

1113
interface Cache
1214
{
13-
public function put(string $key, mixed $value, ?DateTimeInterface $expiresAt = null): CacheItemInterface;
15+
/**
16+
* Whether the cache is enabled.
17+
*/
18+
public bool $enabled {
19+
get;
20+
set;
21+
}
1422

15-
public function get(string $key): mixed;
23+
/**
24+
* Returns a lock for the specified key. The lock is not acquired until `acquire()` is called.
25+
*
26+
* @param Stringable|string $key The identifier of the lock.
27+
* @param null|Duration|DateTimeInterface $expiration The expiration time for the lock. If not specified, the lock will not expire.
28+
* @param null|Stringable|string $owner The owner of the lock, which will be used to identify the process releasing it. If not specified, a random string will be used.
29+
*/
30+
public function lock(Stringable|string $key, null|Duration|DateTimeInterface $expiration = null, null|Stringable|string $owner = null): Lock;
1631

17-
/** @param Closure(): mixed $cache */
18-
public function resolve(string $key, Closure $cache, ?DateTimeInterface $expiresAt = null): mixed;
32+
/**
33+
* Sets the specified key to the specified value in the cache. Optionally, specify an expiration.
34+
*/
35+
public function put(Stringable|string $key, mixed $value, null|Duration|DateTimeInterface $expiration = null): CacheItemInterface;
1936

20-
public function remove(string $key): void;
37+
/**
38+
* Sets the specified keys to the specified values in the cache. Optionally, specify an expiration.
39+
*
40+
* @template TKey of array-key
41+
* @template TValue
42+
*
43+
* @param iterable<TKey,TValue> $array
44+
* @return array<TKey,CacheItemInterface>
45+
*/
46+
public function putMany(iterable $values, null|Duration|DateTimeInterface $expiration = null): array;
2147

22-
public function clear(): void;
48+
/**
49+
* Gets the value associated with the specified key from the cache. If the key does not exist, null is returned.
50+
*/
51+
public function get(Stringable|string $key): mixed;
52+
53+
/**
54+
* Gets the values associated with the specified keys from the cache. If a key does not exist, null is returned for that key.
55+
*
56+
* @template TKey of array-key
57+
* @template TValue
58+
*
59+
* @param iterable<TKey,TValue> $array
60+
* @return array<TValue,mixed>
61+
*/
62+
public function getMany(iterable $key): array;
63+
64+
/**
65+
* Determines whether the cache contains the specified key.
66+
*/
67+
public function has(Stringable|string $key): bool;
68+
69+
/**
70+
* Increments the value associated with the specified key by the specified amount. If the key does not exist, it is created with the specified amount.
71+
*/
72+
public function increment(Stringable|string $key, int $by = 1): int;
2373

24-
public function isEnabled(): bool;
74+
/**
75+
* Decrements the value associated with the specified key by the specified amount. If the key does not exist, it is created with the negative amount.
76+
*/
77+
public function decrement(Stringable|string $key, int $by = 1): int;
78+
79+
/**
80+
* 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.
81+
*/
82+
public function resolve(Stringable|string $key, Closure $callback, null|Duration|DateTimeInterface $expiration = null): mixed;
83+
84+
/**
85+
* Removes the specified key from the cache.
86+
*/
87+
public function remove(Stringable|string $key): void;
88+
89+
/**
90+
* Clears the entire cache.
91+
*/
92+
public function clear(): void;
2593
}

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
}

0 commit comments

Comments
 (0)