Support » Plugin: WP REST Cache » GET /wp-json/oembed… not cached – can it be?

  • Resolved blakespot

    (@blakespot)


    Ever since I started using Mastodon in addition to my other social networks to share my WP blog posts, my server is getting absolutely floored by the hundreds of Masto instances (servers) doing GET requests for thumbnail image, summary text, etc. — basically all at the same time, immediately after I share the link. For about four minutes the server is crushed.

    This an example of the hundreds of request that hit the server:

    "GET /wp-json/oembed/1.0/embed?url=https://bytecellar.com/2022/12/25/have-a-helping-of-8-bit-holiday-cheer-2022-edition/ HTTP/1.1" 500 2604 "-" "http.rb/5.1.0 (Mastodon/4.0.2; +https://mastodon.scot/)"

    I was hoping this was one of the standard REST calls that would be automatically cached, but looking at load on my most recent post share after installing the plug-in (and it is listed in the must-use area, as well), it seems it’s not.

    I use Yoast SEO plug-in which manages OpenGraph sort-of info, FYI.

    Can this plug-in cache this type of call? Many thanks.

    • This topic was modified 1 month, 2 weeks ago by blakespot. Reason: Spelling
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @blakespot

    Thank you for using our plugin!

    No the oembed endpoint isn’t part of the standard REST calls that are automatically cached. Basically we cache endpoints starting with /wp-json/wp/v2 that are publically available (i.e. you don’t need authentication to use them). To be honest I wasn’t aware the oembed endpoint was part of WordPress core.

    Having said that: now I know of this endpoint we will be adding it to our default cached endpoints in the future. For now you could add them to the cache by adding the following filter:

    /**
     * Register the /wp-json/oembed/1.0/embed endpoint so it will be cached.
     */
    function wprc_add_oembed_endpoint( $allowed_endpoints ) {
    	if ( ! isset( $allowed_endpoints[ 'oembed/1.0' ] ) || ! in_array( 'embed', $allowed_endpoints[ 'oembed/1.0' ] ) ) {
    		$allowed_endpoints[ 'oembed/1.0' ][] = 'embed';
    	}
    	return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_oembed_endpoint', 10, 1 );

    Please keep in mind that although this enables the caching it doesn’t enable the automatic clearing of the cache. See our FAQ on how to enable detection of the object type (and thus the automatic clearing of the cache): https://wordpress.org/plugins/wp-rest-cache/#on%20the%20cache%20overview%20page%20i%20see%20the%20object%20type%20is%20%27unknown%27.%20can%20i%20help%20the%20wp%20rest%20cache%20plugin%20to%20detect%20the%20object%20type%20correctly%3F

    Thread Starter blakespot

    (@blakespot)

    Thank you for this code and the plan to make this a standard cached endpoint in a future version.

    I gave this a test and it does help alleviate my load issue. There are far fewer oembed requests (presumably failed requests due to load, previously, were repeated with frequency) and the load was noticeably lower. I see the stats for this in the Endpoint API Caches tab for the plug-in.

    Here is a sample of the access log as I was being hit by the Mastodon instances after the above code was added.

    Masto instances are still hammering the server for the direct link, and I do have W3 Supercache installed, but it is not in the most effective mode, it’s redirect mode — I will need to look into switching to that mode, but that’s nothing to do with this thread.

    Thanks again!

    Thread Starter blakespot

    (@blakespot)

    ( And here is a sample of the access logs during the incoming Mastodon requests before the code was put in place — note the higher number of oembed requests, which presumable are repeats after a 500 response, earlier.

    Update: And here is a proper text file showing another snippet of the access logs with the code in place and WP Supercache now in mod_rewrite mode. )

    • This reply was modified 1 month, 1 week ago by blakespot.
    Plugin Author Richard Korthuis

    (@rockfire)

    HI @blakespot

    We just released a new version of our plugin which includes caching of oembed endpoint including automatic flushing.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.