@@ -43,7 +43,15 @@ class RestDispatch implements WpHooksInterface
43
43
const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete ' ;
44
44
const QUERY_CACHE_REFRESH = 'rest_cache_refresh ' ;
45
45
46
- const VERSION = '1.2.0 ' ;
46
+ const VERSION = '1.2.2 ' ;
47
+
48
+ /**
49
+ * Has the current request been cached? Avoids the multi loop calls where
50
+ * multiple objects are in one endpoint.
51
+ *
52
+ * @var bool[] $cached If the current requested dispatch has been cached.
53
+ */
54
+ private static $ cached ;
47
55
48
56
/**
49
57
* Add class hooks.
@@ -70,8 +78,10 @@ protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request
70
78
$ group = $ this ->getCacheGroup ();
71
79
$ key = $ this ->getCacheKey ($ request_uri , $ server , $ request );
72
80
73
- // Don't cache non-readable (GET) methods.
74
- if ($ request ->get_method () !== WP_REST_Server::READABLE ) {
81
+ // Return the result if it's a non-readable (GET) method or it's been cached.
82
+ if ($ request ->get_method () !== WP_REST_Server::READABLE ||
83
+ (! empty (self ::$ cached [$ this ->cleanKey ($ key )]) && self ::$ cached [$ this ->cleanKey ($ key )] === true )
84
+ ) {
75
85
return $ result ;
76
86
}
77
87
@@ -202,6 +212,7 @@ protected function getCachedResult(
202
212
bool $ force = false
203
213
) {
204
214
$ result = \wp_cache_get ($ this ->cleanKey ($ key ), $ group , $ force );
215
+ self ::$ cached [$ this ->cleanKey ($ key )] = $ result !== false ;
205
216
if ($ result === false ) {
206
217
$ result = $ this ->dispatchRequest ($ server , $ request );
207
218
$ defaults = [
@@ -222,7 +233,12 @@ protected function getCachedResult(
222
233
($ options [Settings::EXPIRATION ][Settings::PERIOD ] * $ options [Settings::EXPIRATION ][Settings::LENGTH ]),
223
234
$ options [Settings::EXPIRATION ]
224
235
);
225
- \wp_cache_set ($ this ->cleanKey ($ key ), $ result , $ group , \absint ($ expire ));
236
+ self ::$ cached [$ this ->cleanKey ($ key )] = \wp_cache_set (
237
+ $ this ->cleanKey ($ key ),
238
+ $ result ,
239
+ $ group ,
240
+ \absint ($ expire )
241
+ );
226
242
227
243
return $ result ;
228
244
}
0 commit comments