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