|
6 | 6 |
|
7 | 7 | use Closure; |
8 | 8 | use Psr\Cache\CacheItemInterface; |
| 9 | +use Stringable; |
9 | 10 | use Tempest\DateTime\DateTimeInterface; |
| 11 | +use Tempest\DateTime\Duration; |
10 | 12 |
|
11 | 13 | interface Cache |
12 | 14 | { |
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 | + } |
14 | 22 |
|
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; |
16 | 31 |
|
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; |
19 | 36 |
|
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; |
21 | 47 |
|
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; |
23 | 73 |
|
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 | + * @var null|Duration $stale Allow the value to be stale for the specified amount of time in addition to the time-to-live specified by `$expiration`. When a value is stale, it will still be returned as-is, but it will be refreshed in the background. |
| 83 | + */ |
| 84 | + public function resolve(Stringable|string $key, Closure $callback, null|Duration|DateTimeInterface $expiration = null, ?Duration $stale = null): mixed; |
| 85 | + |
| 86 | + /** |
| 87 | + * Removes the specified key from the cache. |
| 88 | + */ |
| 89 | + public function remove(Stringable|string $key): void; |
| 90 | + |
| 91 | + /** |
| 92 | + * Clears the entire cache. |
| 93 | + */ |
| 94 | + public function clear(): void; |
25 | 95 | } |
0 commit comments