| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Contract\Cache; |
| 4: | |
| 5: | use Psr\SimpleCache\CacheInterface as PsrCacheInterface; |
| 6: | use Salient\Contract\Core\Instantiable; |
| 7: | use DateInterval; |
| 8: | use DateTimeInterface; |
| 9: | |
| 10: | /** |
| 11: | * @api |
| 12: | */ |
| 13: | interface CacheInterface extends PsrCacheInterface, Instantiable |
| 14: | { |
| 15: | /** |
| 16: | * Store an item in the cache |
| 17: | * |
| 18: | * @param string $key |
| 19: | * @param mixed $value |
| 20: | * @param DateTimeInterface|DateInterval|int|null $ttl The value's TTL in |
| 21: | * seconds or as a {@see DateInterval}, a {@see DateTimeInterface} |
| 22: | * representing its expiration time, or `null` if it should be cached |
| 23: | * indefinitely. |
| 24: | * |
| 25: | * Providing a TTL less than or equal to 0 seconds has the same effect as |
| 26: | * calling `delete($key)`. |
| 27: | * @return true |
| 28: | */ |
| 29: | public function set($key, $value, $ttl = null): bool; |
| 30: | |
| 31: | /** |
| 32: | * Check if an item is present in the cache and has not expired |
| 33: | * |
| 34: | * @param string $key |
| 35: | */ |
| 36: | public function has($key): bool; |
| 37: | |
| 38: | /** |
| 39: | * Retrieve an item from the cache |
| 40: | * |
| 41: | * @param string $key |
| 42: | * @param mixed $default |
| 43: | * @return mixed `$default` if the item has expired or doesn't exist. |
| 44: | */ |
| 45: | public function get($key, $default = null); |
| 46: | |
| 47: | /** |
| 48: | * Retrieve an instance of a class from the cache |
| 49: | * |
| 50: | * @template T of object |
| 51: | * |
| 52: | * @param string $key |
| 53: | * @param class-string<T> $class |
| 54: | * @param T|null $default |
| 55: | * @return T|null `$default` if the item has expired, doesn't exist or is |
| 56: | * not an instance of `$class`. |
| 57: | */ |
| 58: | public function getInstanceOf($key, string $class, ?object $default = null): ?object; |
| 59: | |
| 60: | /** |
| 61: | * Retrieve an array from the cache |
| 62: | * |
| 63: | * @param string $key |
| 64: | * @param mixed[]|null $default |
| 65: | * @return mixed[]|null `$default` if the item has expired, doesn't exist or |
| 66: | * is not an array. |
| 67: | */ |
| 68: | public function getArray($key, ?array $default = null): ?array; |
| 69: | |
| 70: | /** |
| 71: | * Retrieve an integer from the cache |
| 72: | * |
| 73: | * @param string $key |
| 74: | * @return int|null `$default` if the item has expired, doesn't exist or is |
| 75: | * not an integer. |
| 76: | */ |
| 77: | public function getInt($key, ?int $default = null): ?int; |
| 78: | |
| 79: | /** |
| 80: | * Retrieve a string from the cache |
| 81: | * |
| 82: | * @param string $key |
| 83: | * @return string|null `$default` if the item has expired, doesn't exist or |
| 84: | * is not a string. |
| 85: | */ |
| 86: | public function getString($key, ?string $default = null): ?string; |
| 87: | |
| 88: | /** |
| 89: | * Delete an item from the cache |
| 90: | * |
| 91: | * @param string $key |
| 92: | * @return true |
| 93: | */ |
| 94: | public function delete($key): bool; |
| 95: | |
| 96: | /** |
| 97: | * Delete all items from the cache |
| 98: | * |
| 99: | * @return true |
| 100: | */ |
| 101: | public function clear(): bool; |
| 102: | |
| 103: | /** |
| 104: | * Store multiple items in the cache |
| 105: | * |
| 106: | * @param iterable<string,mixed> $values |
| 107: | * @param DateTimeInterface|DateInterval|int|null $ttl |
| 108: | * @return true |
| 109: | */ |
| 110: | public function setMultiple($values, $ttl = null): bool; |
| 111: | |
| 112: | /** |
| 113: | * Retrieve multiple items from the cache |
| 114: | * |
| 115: | * @param iterable<string> $keys |
| 116: | * @param mixed $default |
| 117: | * @return iterable<string,mixed> |
| 118: | */ |
| 119: | public function getMultiple($keys, $default = null); |
| 120: | |
| 121: | /** |
| 122: | * Delete multiple items from the cache |
| 123: | * |
| 124: | * @param iterable<string> $keys |
| 125: | * @return true |
| 126: | */ |
| 127: | public function deleteMultiple($keys): bool; |
| 128: | |
| 129: | /** |
| 130: | * Get the number of unexpired items in the cache |
| 131: | */ |
| 132: | public function getItemCount(): int; |
| 133: | |
| 134: | /** |
| 135: | * Get a list of unexpired items in the cache |
| 136: | * |
| 137: | * @return string[] |
| 138: | */ |
| 139: | public function getItemKeys(): array; |
| 140: | |
| 141: | /** |
| 142: | * Get a copy of the cache where items do not expire over time |
| 143: | * |
| 144: | * Returns an instance where items expire relative to the time of the call |
| 145: | * to the method, allowing clients to mitigate race conditions like items |
| 146: | * expiring or being replaced between subsequent calls. |
| 147: | * |
| 148: | * Only one copy of the cache can be open at a time. Copies are closed via |
| 149: | * {@see close()} or by going out of scope. |
| 150: | * |
| 151: | * @param int|null $now If given, items expire relative to this Unix |
| 152: | * timestamp instead of the time the method is called. |
| 153: | * @return static |
| 154: | * @throws CacheCopyFailedException if the cache is an instance returned by |
| 155: | * {@see asOfNow()}, or if another copy of the cache is open. |
| 156: | */ |
| 157: | public function asOfNow(?int $now = null): self; |
| 158: | |
| 159: | /** |
| 160: | * Close the cache and any underlying resources |
| 161: | * |
| 162: | * If the cache is an instance returned by {@see asOfNow()}, the original |
| 163: | * instance remains open after any locks held by the copy are released. |
| 164: | */ |
| 165: | public function close(): void; |
| 166: | } |
| 167: |