Title: Cache doesn&#8217;t work with WP_REST_Request
Last modified: February 9, 2023

---

# Cache doesn’t work with WP_REST_Request

 *  [emmanueldch](https://wordpress.org/support/users/emmanueldch/)
 * (@emmanueldch)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/cache-doesnt-work-with-wp_rest_request/)
 * Hi there,
 * Firstly, let me thank you for your very helpful plugin.
 * However, I am encountering an issue. 
   I have a page template (basically a front-
   end sitemap) that is listing all my products – which is a custom post type (not
   related to woocommerce).In order to do so, I am using my custom API point /tree
   that is already listing all of my products the way I need to.
 * I noticed that when my /tree has been flushed (code below).
 *     ```wp-block-code
       add_filter('acf/save_post', function () {
           // Systematically flush /tree route since this route is retrieving every pages of the website
           Caching::get_instance()->delete_cache_by_endpoint('/wp-json/easyfichiers/v1/tree', Caching::FLUSH_LOOSE);
   
       [...]
   
       }, 10);
       ```
   
 * My /tree route doesn’t get cached when I am calling it through the WordPress 
   built-in WP_Rest_Request class (code below).
 *     ```wp-block-code
           public static function tree()
           {
               $request = new WP_REST_Request('GET', '/easyfichiers/v1/tree');
               $response = rest_do_request($request);
               $server = rest_get_server();
               $data = $server->response_to_data($response, false);
   
               return $data['response'];
           }
       ```
   
 * So my questions are :
   – How to get my /tree route cached when calling it through
   WP_Rest_Request.– I asked myself another question as I was writing this post :
   does WP_Rest_Request even uses WP Rest Cache to retrieve my custom route content?
 * Thanks in advance
   Emmanuel

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Richard Korthuis](https://wordpress.org/support/users/rockfire/)
 * (@rockfire)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/cache-doesnt-work-with-wp_rest_request/#post-16455862)
 * Hi [@emmanueldch](https://wordpress.org/support/users/emmanueldch/)
 * Thank you for using our plugin!
   To answer your last question first: No the WP_Rest_Request
   does not use WP REST Cache to retrieve any endpoint. The way our plugin is set
   up it only caches external calls to the REST API and not calls via the WP_Rest_Request.
   But let me return with a question for you: Do you need caching for these type
   of calls? I just did some tests on my local test environment with calls to default
   WP endpoints and calls via the WP_Rest_Request took less than one milisecond,
   where the same call through the external API (so for example to: [https://example.test/wp-json/wp/v2/posts](https://example.test/wp-json/wp/v2/posts))
   took almost 2 seconds. I am just wondering if considering to add caches to the
   WP_Rest_Request calls is worth the time.
 *  Thread Starter [emmanueldch](https://wordpress.org/support/users/emmanueldch/)
 * (@emmanueldch)
 * [3 years, 3 months ago](https://wordpress.org/support/topic/cache-doesnt-work-with-wp_rest_request/#post-16459370)
 * Hi, tha,ks for your answer.
 * To answer your question, let me tell you, my custom /tree route is … **HEAVY**
   unfortunately.
   With the cache system of your plugin I have good results in terms
   of execution time when calling externally. But with a WP_Rest_Request call I 
   have crappy results (~4.5s).
 * 
   Measurements were done like this (on localhost) :
 *     ```wp-block-code
       public static function tree()
           {
               // Starting clock time in seconds
               $start_time = microtime(true);
               $a = 1;
   
               // Start loop
               for ($i = 1; $i <= 1000; $i++) {
                   $a++;
               }
   
               echo " Execution time of script = " . $execution_time . " sec";
               $request = new WP_REST_Request('GET', '/myNamespace/v1/tree');
               $response = rest_do_request($request);
               $server = rest_get_server();
               $data = $server->response_to_data($response, false);
   
               // End clock time in seconds
               $end_time = microtime(true);
   
               var_dump(($end_time - $start_time));
               die();
   
               return $data['response'];
           }
       ```
   
 * I also measured (in the same function) a random call to /wp/v2/pages and I obtained
   similar results as yours.
 * That’s why I wanted to see if your plugin offered cache even with internal calls.
 * 
   If so, I would also have made a “fake” call right after the line were I flush
   my /tree cache to ALWAYS have that route cached.
 *     ```wp-block-code
       add_filter('acf/save_post', function () {
           // Systematically flush /tree route since this route is retrieving every pages of the website
           Caching::get_instance()->delete_cache_by_endpoint('/wp-json/easyfichiers/v1/tree', Caching::FLUSH_LOOSE);
   
       // fake call right here <<<<<
   
       [...]
   
       }, 10);
       ```
   
 * Do you have any workaround to offer ?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Cache doesn’t work with WP_REST_Request’ is closed to new replies.

 * ![](https://ps.w.org/wp-rest-cache/assets/icon-256x256.png?rev=3328849)
 * [WP REST Cache](https://wordpress.org/plugins/wp-rest-cache/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-rest-cache/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-rest-cache/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-rest-cache/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-rest-cache/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-rest-cache/reviews/)

## Tags

 * [cache](https://wordpress.org/support/topic-tag/cache/)

 * 2 replies
 * 2 participants
 * Last reply from: [emmanueldch](https://wordpress.org/support/users/emmanueldch/)
 * Last activity: [3 years, 3 months ago](https://wordpress.org/support/topic/cache-doesnt-work-with-wp_rest_request/#post-16459370)
 * Status: not resolved