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

    reading the thread title - the first thing that comes into mind is - this guy hasn't tuned off caching for his snippets! Unfortunately this isn't the case - I'm calling both snippets uncached.

    I try to implement a simple click counter in MODx Revo and therefore I made two simple snippets:

    Snippet ClickCounterSet which increases and writes the TV value of the TV clickCounter

    <?php
    $tv = $modx->getObject('modTemplateVar',array('name'=>'clickCounter'));
    if (!$tv) {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'TV clickCounter not set');
        return;
    }
    
    $id       = $modx->resource->get('id');
    $page     = $modx->getObject('modResource', $id);
    $newVal   = $page->getTVValue('clickCounter') + 1;
    
    if (!$page->setTVValue('clickCounter', $newVal)) {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not save TV clickCounter');
    }
    
    return;
    


    and Snippet ClickCounterGet which reads and returns the TV value of the TV clickCounter

    <?php
    $tv = $modx->getObject('modTemplateVar',array('name'=>'clickCounter'));
    if (!$tv) return '(TV clickCounter not set)';
    
    $id = isset($resourceid) ? $resourceid : $modx->resource->get('id');
    
    $page = $modx->getObject('modResource', $id);
    $clicks = $page->getTVValue('clickCounter');
    
    return ($clicks > 0) ? $clicks : 0;
    


    Both snippets are called uncached but the value is only increased once (when the resource is loaded the first time after cache is cleared). Reloading the resource doesn't write the increased value to the TV. Clearing the cache causes the write process to work once again - and so on ...

    What am I doing wrong? This thing slowly drives me nuts...

    By the way: no message is written to the MODx system log!
    Using MODx Revo 2.2.4-pl [ed. note: gadgetto last edited this post 14 years, 3 months ago.]
      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
      • 1778
      • 659 Posts
      Hi
      Both snippets are called uncached but what about the resource you call them from? Is it cacheable ? the check box Clear cache is it activated ?
      Hope this helps
      Cheers
        • 10378
        • 375 Posts
        I'm using these snippets within templates or chunks for Articles resources. Didn't find a setting to enable/disable caching for Article resources.

        But I thought the caching behavior of MODx is granular - a resource can be cached and nevertheless parts of its chunks/templates/snippet calls can be uncached?
          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/
          • 32316
          • 387 Posts
          But I thought the caching behavior of MODx is granular - a resource can be cached and nevertheless parts of its chunks/templates/snippet calls can be uncached?
          It is - If you open a cache file for a page you will find uncached snippet calls in the html code - in fact that may be a good thing to check, look for the cached page and make sure it has the snippet call in it (which should then be parsed when viewing the cached page)
            • 3749
            • 24,544 Posts
            One thing to check is the modx_site_tmplvar_contentvalues table. Find the record where the tmplvarid is the ID of the TV and the resourceid is the ID of the resource. When you visit the page with the counter, the value in the DB should change when you reload the PhpMyAdmin page.

            I strongly suspect that the DB value is changing every time, but MODX (or your browser) is caching the result.

            If the DB is OK, you could try visiting from another browser.


            Another thing to try would be calling the chunk containing the snippet tag uncached (actually -- try that first):

            [[!$ChunkName]]




            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.html
              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
              • 10378
              • 375 Posts
              Quote from: whistlemaker at Jun 23, 2012, 09:58 PM
              But I thought the caching behavior of MODx is granular - a resource can be cached and nevertheless parts of its chunks/templates/snippet calls can be uncached?
              It is - If you open a cache file for a page you will find uncached snippet calls in the html code - in fact that may be a good thing to check, look for the cached page and make sure it has the snippet call in it (which should then be parsed when viewing the cached page)

              The cache files for resources should be in core/cache/resource - right?
              I can only find .php files in there. Should there be static .html files?
                Freelancer @bitego http://www.bitego.com
                ---
                GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                More infos here: http://www.bitego.com/extras/goodnews/
                • 10378
                • 375 Posts
                Hi Bob,

                Quote from: BobRay at Jun 24, 2012, 12:36 AM
                One thing to check is the modx_site_tmplvar_contentvalues table. Find the record where the tmplvarid is the ID of the TV and the resourceid is the ID of the resource. When you visit the page with the counter, the value in the DB should change when you reload the PhpMyAdmin page.

                I strongly suspect that the DB value is changing every time, but MODX (or your browser) is caching the result.

                This was the first thing I checked - but the value of the TV doesn't change after its called once.

                Quote from: BobRay at Jun 24, 2012, 12:36 AM

                If the DB is OK, you could try visiting from another browser.

                Tried but same result. The problem lies definitely inside MODx caching system.

                Quote from: BobRay at Jun 24, 2012, 12:36 AM

                Another thing to try would be calling the chunk containing the snippet tag uncached (actually -- try that first):

                [[!$ChunkName]]


                The snippet call is currently directly inside a template not a chunk. But I will try to place the snippet call inside a chunk.

                Thanks for your help!
                  Freelancer @bitego http://www.bitego.com
                  ---
                  GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                  More infos here: http://www.bitego.com/extras/goodnews/
                  • 10378
                  • 375 Posts
                  OK - I think I have nailed down the problem:


                  It only doesn't work within Articles resources! Regardless if I call the snippets within the Articles template or chunk or inside the resource itself.
                  Calling the snippets in "normal" MODx resources works like a charm!

                  Is there anything special about Articles caching? I couldn't find a setting in the Articles container regarding caching behavior.
                    Freelancer @bitego http://www.bitego.com
                    ---
                    GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                    More infos here: http://www.bitego.com/extras/goodnews/
                    • 10378
                    • 375 Posts
                    Quote from: BobRay at Jun 24, 2012, 12:36 AM
                    Another thing to try would be calling the chunk containing the snippet tag uncached (actually -- try that first):

                    [[!$ChunkName]]


                    Placed the uncached snippet call into a chunk und called this chunk uncached - but still same result - Value in TV gets updated only once (and each time I clear the cache).
                    Must definitely be a strange caching behavior in Articles resources!
                      Freelancer @bitego http://www.bitego.com
                      ---
                      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
                      More infos here: http://www.bitego.com/extras/goodnews/
                      • 32316
                      • 387 Posts
                      The cache files for resources should be in core/cache/resource - right?
                      I can only find .php files in there. Should there be static .html files?
                      They are .php files - any uncached calls have to parsed and the php code run.
                      The cache is not as simple as just a static html version of the page - much more complex!

                      It only doesn't work within Articles resources! ...

                      ... Is there anything special about Articles caching?

                      Interesting, and sorry I have no idea.