• Hi, thank you for your plugin, it is very useful, I can cache my custom api through this following method, but now I have a need to determine whether to cache this api(wp/v2/getproduct) for one request time. Because once this api triggers certain conditions, I cannot cache this time api cache. Is there a method to achieve it in my custom api code?

    function wprc_add_acf_posts_endpoint($allowed_endpoints)
    {
        if (!isset($allowed_endpoints['wp/v2']) ||
            !in_array('getcontextualbundle', $allowed_endpoints['wp/v2'])) {
            $allowed_endpoints['wp/v2'][] = 'getproduct';
        }
        return $allowed_endpoints;
    }
    //determine_cache_object_type
    function wprc_determine_object_type($object_type, $cache_key, $data, $uri)
    {
    
        if ($object_type !== 'unknown') {
            return $object_type;
        }
        if (strpos($uri, 'wp/v2/getproduct')) {
            $object_type = 'product';
        }
    
        return $object_type;
    }
    
    add_filter('wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
    
    /***
     *
     * @description: my custom api
     */
    
    function getproduct()
    {
    
    if(true)
    {
      How to not cache this access api?
    }
    
    }

    • This topic was modified 1 year, 4 months ago by superjin001.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @superjin001

    You have to keep in mind that whenever a cache record exists for the current request, the cache is returned and no other code is executed. So no filters inside plugins or inside the theme. This makes your question tricky.

    My solution would be to make use of the wp_rest_cache/skip_caching filter. This should however be used inside a mu-plugin which comes alphabetically before wp-rest-cache.php (This is because of the way our plugin works). If you use this filter and have it return true in your use case, our plugin will not return a cache record.

    Thread Starter superjin001

    (@superjin001)

    Hi @richard, thank you for your reply, sorry, I probably didn’t describe my needs clearly.
    I have implemented caching for my custom API as your document. I want to determine within my custom API whether this particular request is cached or not when there is no cache for it. My custom API(getinfo) relies on an external website’s API, which can sometimes be unreliable. I want to avoid caching my API when this external API is not functioning correctly.

    function getinfo() {
        //   Send request to external API
        $external_api_response = wp_remote_get('https://external-api.com/data');
    
        // Check if the external API response is valid
        if (is_wp_error($external_api_response) || wp_remote_retrieve_response_code($external_api_response) !== 200) {
           
          
            add_filter('wp_rest_cache/skip_caching', '__return_true'); 
            add_filter('wp_rest_cache/skip_caching', 'true'); 
            add_filter('wp_rest_cache/skip_caching', true); 
        }
    ..............other code............
    }
    
    // Register custom API routing
    add_action('rest_api_init', function() {
        register_rest_route('wp/v2', '/getinfo/', array(
            'methods' => 'GET',
            'callback' => 'getinfo',
        ));
    });
    
    
    
    /**
     * Register the /wp-json/wp/v2/getinfo endpoint so it will be cached.
     */
    function wprc_add_acf_posts_endpoint($allowed_endpoints)
    {
    
        if (!isset($allowed_endpoints['wp/v2']) ||
            !in_array('getinfo', $allowed_endpoints['wp/v2'])) {
            $allowed_endpoints['wp/v2'][] = 'getinfo';
        }
    
        
        return $allowed_endpoints;
    }
    
    add_filter('wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
    

    As shown in the code, I tried these three( add_filter(‘wp_rest_cache/skip_caching’, ‘__return_true’); add_filter(‘wp_rest_cache/skip_caching’, ‘true’); add_filter(‘wp_rest_cache/skip_caching’, true); ) but none of them worked, could you please tell me any way to meet my needs, thank you in advance!

    • This reply was modified 1 year, 4 months ago by superjin001.
    • This reply was modified 1 year, 4 months ago by superjin001.
    • This reply was modified 1 year, 4 months ago by superjin001.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @superjin001

    Unfortunately that won’t work, because you would be adding the filter AFTER the filter was checked. That filter can only be used from a mu-plugin.

    I did check if I could think of a way how you could achieve what you want, but unfortunately at this point I don’t have a solution for you. (The only way you could prevent caching from within your function would be to either return an empty result set (I don’t think that’s an option) or to return a status code unequal to 200.)
    Another option would be to remove the filter we add to save it to the cache. Unfortunately I just found out that simply using remove_filter doesn’t work because of the way we add the filter (with $this from within a class) ??

    I will see if I can add something to make it possible, but I cannot tell you how soon I will be able to release it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘is It possible to determine whether to maintain this cache in my api code’ is closed to new replies.