@@ -26,17 +26,10 @@ class Cache_Middleware {
2626 /**
2727 * Constructor.
2828 *
29- * @throws \InvalidArgumentException If the TTL is not valid.
30- *
31- * @param int|DateTimeInterface|callable $ttl Time to live for the cache.
29+ * @param int|DateTimeInterface|Closure $ttl Time to live for the cache.
30+ * @param string|null $key Cache key to use.
3231 */
33- public function __construct ( protected mixed $ ttl ) {
34- if ( ! is_int ( $ ttl ) && ! $ ttl instanceof DateTimeInterface && ! is_callable ( $ ttl ) ) { // @phpstan-ignore-line
35- throw new \InvalidArgumentException (
36- 'TTL must be an integer, DateTimeInterface, or a callable that returns an integer. '
37- );
38- }
39- }
32+ public function __construct ( protected int |DateTimeInterface |Closure $ ttl , public readonly ?string $ key = null ) {}
4033
4134 /**
4235 * Invoke the middleware.
@@ -77,7 +70,7 @@ public function purge( Pending_Request $request ): bool {
7770 * @param Pending_Request $request Request to retrieve the cache key for.
7871 */
7972 protected function get_cache_key ( Pending_Request $ request ): string {
80- return md5 ( (string ) json_encode ( [ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
73+ return $ this -> key ?? md5 ( (string ) json_encode ( [ // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode
8174 $ request ->base_url (),
8275 $ request ->url (),
8376 $ request ->method (),
@@ -89,24 +82,35 @@ protected function get_cache_key( Pending_Request $request ): string {
8982 /**
9083 * Calculate the time to live for the cache in seconds.
9184 *
92- * @throws \InvalidArgumentException If the TTL callback returns an invalid value.
93- *
9485 * @param Pending_Request $request Request to calculate the TTL for.
9586 * @param Response $response Response to calculate the TTL for.
9687 */
9788 private function calculate_ttl ( Pending_Request $ request , Response $ response ): int {
98- if ( is_callable ( $ this ->ttl ) ) {
99- $ callback = $ this ->ttl ;
89+ $ ttl = $ this ->ttl ;
10090
101- $ value = $ callback ( $ request , $ response );
91+ if ( $ ttl instanceof Closure ) {
92+ $ ttl = $ this ->invoke_expiration_callback ( $ ttl , $ request , $ response );
93+ }
10294
103- if ( ! is_numeric ( $ value ) || (int ) $ value < 0 ) {
104- throw new \InvalidArgumentException ( 'TTL callback must return a non-negative integer. ' );
105- }
95+ return normalize_cache_ttl ( $ ttl );
96+ }
97+
98+ /**
99+ * Invoke the callback with the request and response and validate the return type.
100+ *
101+ * @throws \InvalidArgumentException If the callback does not return an integer or DateTimeInterface.
102+ *
103+ * @param Closure $callback Callback to invoke.
104+ * @param Pending_Request $request Request to pass to the callback.
105+ * @param Response $response Response to pass to the callback.
106+ */
107+ protected function invoke_expiration_callback ( Closure $ callback , Pending_Request $ request , Response $ response ): int |DateTimeInterface {
108+ $ value = $ callback ( $ request , $ response );
106109
107- return (int ) $ value ;
110+ if ( ! is_int ( $ value ) && ! $ value instanceof DateTimeInterface ) {
111+ throw new \InvalidArgumentException ( 'Callback must return an integer or DateTimeInterface. ' );
108112 }
109113
110- return normalize_cache_ttl ( $ this -> ttl ) ;
114+ return $ value ;
111115 }
112116}
0 commit comments