We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40981
    • 8 Posts
    Hey forums, long time no talk! I've got a script that's running quite slow and was wondering if anyone had any thoughts how to speed it up. Here's the offender:

    // RETRIEVE FROM CACHE IF PRESENT
    $result       = '';
    $cacheAge     = 3600;
    $cacheKey     = 'integration';
    $cacheOptions = array(xPDO::OPT_CACHE_KEY => $cacheKey);
    $hash         = $cacheKey . '-' .  $type;
    $cacheHit     = $modx->cacheManager->get($hash, $cacheOptions);
    if ( false === is_null($cacheHit) ) {
    	return $cacheHit;
    }
    
    ... cURL to the other side of the world
    ... so many getObjects
    
    // STORE IN CACHE
    $modx->cacheManager->set($hash, $result, $cacheAge, $cacheOptions);
    
    // RETURN RESULT
    return $result;
    


    The operations in the middle of the script are quite expensive, but my understanding is that everything should be cached and hunky-dory. Indeed, cache files are generated and returned as expected, but that simple action of returning items from the cache seems to be drastically increasing page load. Grabbing something from the cache and returning it takes 15 seconds! Just by commenting out that one snippet, page load goes to a respectable 1.5s.

    What am I doing wrong? Any help would be much appreciated.

    Thank you very much!

    Scott
      • 26681 MODX Staff
      • 123 Posts
      What kind of cache provider is it? Default xPDO File Cache? If so what type of file system? Specifically is it a shared mount? What versions of server software and php are you running?

      Sorry for all the questions lol
        [sepiariver.com] (https://sepiariver.com/)
        • 22303 MODX Staff
        • 10,725 Posts
        What are you connecting to with cURL? The problem is most likely with the cURL call, not the caching. Also, the key for the cache item is not the same as the key for the cache provider—I think you might be getting those confused. Also, what is the $type variable which is being used to create the $hash variable?
          • 40981
          • 8 Posts
          Hey gents, thanks for the replies!

          @SepiaRiver:
          Cache Provider
          Yep, using default xPDO File Cache. On this server, APC is an option too -- think that might have anything to do with it?

          Shared Mount
          I'm not 100% sure what you mean by this, but this is on a shared host. Definitely not the absolute fastest server, but usually usable!

          PHP Version
          Using 5.5... 5.6 is available to me, I'll give that a try this AM. Might be able to get 7 as well -- you know if MODX plays nice with 7?

          Server Software
          Apache is all I know... can dig into a specific version if you think it could be a culprit!

          @opengeek
          cURL Connection
          Connecting to some google drive XML documents that are being used to generate a view on the frontend. It seems strange to me that cURL would be the culprit -- doesn't the script exit when there's a cache hit? Indeed, when I toss a "return true;" on line 1 of the script, the page loads very quick, but when I put it on line 11, it's back to its slow load behaviour. cURL indeed slows things down when it has to be built, but shouldn't the cache circumvent it?

          $type
          There's four different endpoints that cURL is connecting to. This script is being used to connect to one of the four based on which $type it is. This is just a string passed to the snippet when it's called from the template.

          cacheKey
          I believe this is behaving as expected. This code is based on "Example 2" in "Programmatic Caching" in https://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/caching. I am aware that the two keys are different -- In my file system, there is an "integration" folder with four different files that correspond to the four types above. Does this make sense, or do you think I am indeed mixing them up?

          @gallenkamp
          MODX Version
          My favourite version, 2.4.3!

          Thanks!!
            • 26681 MODX Staff
            • 123 Posts
            Scott out of curiosity what happens when you manually modify the content of the generated cache file so that it returns an empty string? Is the return on line 11 still slow?
            What's the file size on the cache file? Also if you include it manually into a php script that does nothing else, what happens? Does that script take a long time?
              [sepiariver.com] (https://sepiariver.com/)
              • 40981
              • 8 Posts
              Quote from: sepiariver at Mar 17, 2016, 02:22 AM
              Scott out of curiosity what happens when you manually modify the content of the generated cache file so that it returns an empty string? Is the return on line 11 still slow?
              What's the file size on the cache file? Also if you include it manually into a php script that does nothing else, what happens? Does that script take a long time?

              Hey @sepiariver, things seem to be speedy today. The size of the cache file is only 20kB right now -- it was 300 kB the other day when I was looking at it. Very weird. I did change the getChunk code to follow the advice in this thread: https://forums.modx.com/thread/74071/help-with-getchunk-and-modx-speed-please?page=4#dis-post-462412.

              I don't know why there would be a discrepancy that big in the file, but it appears to be acceptably fast now. Anyway, must just have been a brain fart, thanks for walking me through it! At least I learned a new way to speed up getChunk while banging my head up against the wall smiley