• 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).

    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).

        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

    (@rockfire)

    Hi @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) 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

    (@emmanueldch)

    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) :

    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.

    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.