@@ -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,10 @@ 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 ($ request ->get_method () !== WP_REST_Server::READABLE ||
83+ (! empty (self ::$ cached [$ this ->cleanKey ($ key )]) && self ::$ cached [$ this ->cleanKey ($ key )] === true )
84+ ) {
7585 return $ result ;
7686 }
7787
@@ -202,6 +212,7 @@ protected function getCachedResult(
202212 bool $ force = false
203213 ) {
204214 $ result = \wp_cache_get ($ this ->cleanKey ($ key ), $ group , $ force );
215+ self ::$ cached [$ this ->cleanKey ($ key )] = $ result !== false ;
205216 if ($ result === false ) {
206217 $ result = $ this ->dispatchRequest ($ server , $ request );
207218 $ defaults = [
@@ -222,7 +233,12 @@ protected function getCachedResult(
222233 ($ options [Settings::EXPIRATION ][Settings::PERIOD ] * $ options [Settings::EXPIRATION ][Settings::LENGTH ]),
223234 $ options [Settings::EXPIRATION ]
224235 );
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+ );
226242
227243 return $ result ;
228244 }
0 commit comments