@@ -43,7 +43,15 @@ class RestDispatch implements WpHooksInterface
4343 const QUERY_CACHE_FORCE_DELETE = 'rest_force_delete ' ;
4444 const QUERY_CACHE_REFRESH = 'rest_cache_refresh ' ;
4545
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 ;
4755
4856 /**
4957 * Add class hooks.
@@ -70,8 +78,11 @@ protected function preDispatch($result, WP_REST_Server $server, WP_REST_Request
7078 $ group = $ this ->getCacheGroup ();
7179 $ key = $ this ->getCacheKey ($ request_uri , $ server , $ request );
7280
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 (
83+ $ request ->get_method () !== WP_REST_Server::READABLE ||
84+ (! empty (self ::$ cached [$ this ->cleanKey ($ key )]) && self ::$ cached [$ this ->cleanKey ($ key )] === true )
85+ ) {
7586 return $ result ;
7687 }
7788
@@ -202,6 +213,7 @@ protected function getCachedResult(
202213 bool $ force = false
203214 ) {
204215 $ result = \wp_cache_get ($ this ->cleanKey ($ key ), $ group , $ force );
216+ self ::$ cached [$ this ->cleanKey ($ key )] = $ result !== false ;
205217 if ($ result === false ) {
206218 $ result = $ this ->dispatchRequest ($ server , $ request );
207219 $ defaults = [
@@ -222,7 +234,9 @@ protected function getCachedResult(
222234 ($ options [Settings::EXPIRATION ][Settings::PERIOD ] * $ options [Settings::EXPIRATION ][Settings::LENGTH ]),
223235 $ options [Settings::EXPIRATION ]
224236 );
225- \wp_cache_set ($ this ->cleanKey ($ key ), $ result , $ group , \absint ($ expire ));
237+ self ::$ cached [$ this ->cleanKey ($ key )] = \wp_cache_set (
238+ $ this ->cleanKey ($ key ), $ result , $ group , \absint ($ expire )
239+ );
226240
227241 return $ result ;
228242 }
0 commit comments