We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Is there an easy way in xPDO to be sure of getting an object from the DB rather than the cache?

    It seems that I'm constantly being bitten by instances where a stale version of an object exists in the cache and getObject() or getCollection() pulls the old object. In an extra, I don't want to refresh the whole cache just to get the revised object.

      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
      • 40706
      • 128 Posts
      Isn`t there the 3rd parameter which states the cacheFlag, to say modx that it should always load this object from the database?

      xPDOObject|null getObject (string $className, [xPDOCriteria|array|str|int $criteria = null], [bool|int $cacheFlag = true])
      
        • 3749
        • 24,544 Posts
        AFAIK, cacheFlag determines how long the results will be cached *after* you retrieve the object.
          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
          • 40706
          • 128 Posts
          I would say, thats not correct.

          https://github.com/modxcms/xpdo-core/blob/master/om/xpdoobject.class.php#L414

          If the cacheFlag is false, he does not even check the cache. Also the xPDOCriteria could have the cache Flag. Maybe in your code you have to set the cacheFlag of the xPDOCriteria also to false.
            • 3749
            • 24,544 Posts
            Looking at the code, it appears that you're right. I was going by this:

            * @param boolean|integer $cacheFlag Indicates if the objects should be
                 * cached and optionally, by specifying an integer value, for how many
                 * seconds.



            If we're right, you lose the ability to create/update the cache for the object when you ask for the fresh object.
              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
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: BobRay at Feb 13, 2013, 12:41 PM
              Looking at the code, it appears that you're right. I was going by this:

              * @param boolean|integer $cacheFlag Indicates if the objects should be
                   * cached and optionally, by specifying an integer value, for how many
                   * seconds.



              If we're right, you lose the ability to create/update the cache for the object when you ask for the fresh object.
              Not at all, so long as any save() is called without sending false to the $cacheFlag parameter as well.
                • 3749
                • 24,544 Posts
                Now I'm confused. Doesn't this code mean that for any $cacheFlag value other than false or 0 , you'll be getting the object from the cache rather than a fresh object from the DB?

                if ($xpdo->_cacheEnabled && $criteria->cacheFlag && $cacheFlag) {
                    $row= $xpdo->fromCache($criteria, $className);
                }


                So to get a fresh object, $cacheFlag has to be false. Then this code that updates the cache wouldn't execute, no?

                if (!$fromCache && $cacheFlag && $xpdo->_cacheEnabled) {
                   $xpdo->toCache($criteria, $instance, $cacheFlag);



                What am I missing?
                  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
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Quote from: BobRay at Feb 14, 2013, 06:12 PM
                  Now I'm confused. Doesn't this code mean that for any $cacheFlag value other than false or 0 , you'll be getting the object from the cache rather than a fresh object from the DB?

                  if ($xpdo->_cacheEnabled && $criteria->cacheFlag && $cacheFlag) {
                      $row= $xpdo->fromCache($criteria, $className);
                  }


                  So to get a fresh object, $cacheFlag has to be false. Then this code that updates the cache wouldn't execute, no?

                  if (!$fromCache && $cacheFlag && $xpdo->_cacheEnabled) {
                     $xpdo->toCache($criteria, $instance, $cacheFlag);



                  What am I missing?

                  You are talking about two different things. If you want to bypass the cache, get the object with cacheFlag set to false. The cache will be ignored completely as you are suggesting, even when saving. The intended use is, get the object with the indicated cacheFlag, either from cache if it exists, or not, then, if from the cache, add/update the cached copy if anything changes and is saved.
                    • 3749
                    • 24,544 Posts
                    It sounds like you're agreeing with me. What I said (or meant to say) was that if you get a fresh object from the DB (by bypassing the cache), the cache won't be updated.

                    I think I can imagine a case where you'd want to get a recently changed object from the DB *and* have the cache updated for that object, but to do that you'd have to use a separate operation to update the cache for the object -- I guess by getting the object twice.
                      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
                      • 22303 MODX Staff
                      • 10,725 Posts
                      If you are getting the object with a parameter for a specific caching action, why can't you call save with a different one?