We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27716
    • 126 Posts
    Hi Guys,

    I have a snippet that is pulling a number of resources and displaying them.

    The snippet is using
    $created = $x->get('createdon'); $age = time() - strtotime($created);
    to get the documents created date.

    I'm finding that often the snippet displays an old cached date / time even though im calling the snippet uncached.

    I've tried adding
    $modx->cacheManager->clearCache(array(
        '/resource'
    ));
    at the top of the snippet, which seems to help, but...Is resource the best folder to clear?

    If i take the approach of clearing cache each time the snippet is executed, and the snippet is used widely on the site doesnt that mean the cache is almost useless because its being cleared everytime a user hits the site?

    Is there another approach to ensuring that the snippet will not display the cached version of data such as created date.
      • 22303 MODX Staff
      • 10,725 Posts
      Don't cache the snippet call.
        • 27716
        • 126 Posts
        Hi opengeek,

        The snippet call isnt't cached.

        I'm thinking, it is possibly its a browser cache issue. I will look into a solution to not allowing a browser server a cached version of the page.

        Stuart
          • 22303 MODX Staff
          • 10,725 Posts
          I'm confused, createdon doesn't change so it doesn't matter if it is cached or not...
            • 27716
            • 126 Posts
            Hi Opengeek,

            Im using createdon, creating a time stamp and using it to display X days since published or, x hours since published etc etc,

            So Every time the user loads the page it should change to some degree.

            It hasnt been changing with every load while I have been testing. Thats me reloading the same page frequently.

            Have added this, hopefully will help.
            <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
            <meta http-equiv="Pragma" content="no-cache">
            <meta http-equiv="Expires" content="0">
            
              • 3749
              • 24,544 Posts
              This code works for me -- it changes every time I reload the page. You might see if it works for you to find out whether it's a browser cache issue:

              <?php
              $created = $modx->resource->get('createdon'); $age = time() - strtotime($created);
              
              return $age;
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 27716
                • 126 Posts
                Hi Bob,

                Thanks for your response.

                After looking into it I also saw that the page was checked as cacheable, unchecking that helped. That browser cache code didn't seem to do much.

                Stuart