• Resolved munikho

    (@munikho)


    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!

    • This topic was modified 2 years, 4 months ago by munikho.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Serhiy Zakharchenko

    (@zahardoc)

    Hello @munikho!

    Thank you for bringing it to our attention. I just checked your code and it looks great to me. I’ll include it in the next plugin release.

    Thank you and best regards,
    Sergiy, development team.

    Thread Starter munikho

    (@munikho)

    Great, thank you @zahardoc! That would be great.

    I did just notice a tiny improvement to be made in my code, which is to only set the cache if the filetype is known.

    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;
    }
    Thread Starter munikho

    (@munikho)

    Btw… the plugin used to be one of the most time-consuming on our server. Now it’s not anymore: https://ibb.co/wKFD8hx

    I saw the get_attachment_id_from_url() function is used in one other place. Since it seems to be an expensive function, I would maybe consider replacing it or finding another way.

    Plugin Author Serhiy Zakharchenko

    (@zahardoc)

    @munikho

    That’s good news, thank you for helping our plugin to be better ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Feed super slow due to get_attachment_id_from_url() function’ is closed to new replies.