Feed super slow due to get_attachment_id_from_url() function
-
I noticed my rss feed was loading super slow, and was wondering why. It took 23 seconds, while there were only 20 something items in it.
Digging into the code I found out that the get_attachment_id_from_url() function is the cause, which was called in get_attachment_mimetype().
I’ve made a small change in get_attachment_mimetype() so it’s getting the mimetype without needing to get the attachment_id.
public function get_attachment_mimetype( $attachment = '' ) { // Let's hash the URL to ensure that we don't get any illegal chars that might break the cache. $key = md5( $attachment ); if ( $attachment ) { // Do we have anything in the cache for this? $mime = wp_cache_get( $key, 'mime-type' ); if ( $mime === false ) { // Get the MIME type $filetype = wp_check_filetype($attachment); if($filetype){ $mime = $filetype['type']; } // Set the cache wp_cache_set( $key, $mime, 'mime-type', DAY_IN_SECONDS ); } return $mime; } return false; }
This changed the loading speed from 23 seconds to 1.8 seconds!! Seems like a good improvement.
Could you implement this (or a different solution) so loading the feed is quick? I wanted to submit a pull request on github, but noticed it’s not up to date anymore.
Thank you!
- The topic ‘Feed super slow due to get_attachment_id_from_url() function’ is closed to new replies.