• Resolved superjin001

    (@superjin001)


    Hi, thank your for your plugin, it is very helpful, because my project is highly customized, some rest APIs rely on external interfaces. I hope that this type apis has a shorter cache timeout, while other apis have a longer cache timeout. So may I ask if there is any method to set different cache timeouts for different rest APIs for our wordpress rest api. If we can make it, please let me know, thank you in advance!

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

    (@rockfire)

    Hi @superjin001

    Thank you for using our plugin!

    Yes, you can do something like this:

    /**
     * Change the timeout for different endpoints.
     *
     * @param int $timeout The timeout as set in the settings.
     * @param array $options An array of options, containing the current uri ('uri'), the object type ('object_type'), the request headers ('request_headers') and the request method ('request_method').
     *
     * @return int
     */
    function wprc_variable_timeout( $timeout, $options ) {
    	if ( false !== strpos( 'wp/v2/posts', $options['uri'] ) ) {
    		$timeout = DAY_IN_SECONDS;
    	}
    
    	return $timeout;
    }
    
    add_filter( 'wp_rest_cache/timeout', 'wprc_variable_timeout', 10, 2 );
    Thread Starter superjin001

    (@superjin001)

    Hi @Richard Korthuis?, thank you for your quick reply, I tried the way you told, and it works, I didn’t expect a free plugin can be made so good and support quick Q&A, thank you again!

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @superjin001,

    Good to hear it works for you! I must admit I am not always this quick in answering. You just happened to have send your question just before I opened my email and had time to respond ??

    If you have any more questions, feel free to open a new topic!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘can every cache have different cache timeout’ is closed to new replies.