We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3186 ☆ A M B ☆
    • 279 Posts
    I have made a website with a lot of custom components, and a lot of custom caching. For example I take a "Noticeboard" component. This one is having cache files for the overview listing, search listings and one single cache file for all detail pages. Works like a charm, site is fast with loading the pages.

    Now you can login the frontend and edit your own items. For example let say I want to edit item with ID 15. After I'm done with editting I do a clear cache of my custom components cache like this:

    $partitions = array(
        'customcmps/noticeboard/list' => array(),
        'customcmps/noticeboard/search' => array(),
    );
    $modx->cacheManager->refresh($partitions);


    Which is working great. But I also have a detailpage cache file, which is stored in "customcmps/noticeboard/" itsef, named like "15.detail.cache.php". I do not want to remove all the detail cache. You can imagine that when you have 10.000 items, you don't want to lose all the cache files... So I figured out to use this:

    $modx->cacheManager->delete('customcmps/noticeboard/'.$item->get('id').'.detail');


    Without the ".cache.php" extension.
    But the file isn't removed so the detail cache isn't cleared and the page contains the old unchanged content.

    Can anyone tell me more about this? Thanks in advance!

    Regards, Bert
      MODX Ambassador (NL) & Professional MODX developer
      Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
      MODX e-commerce solution SimpleCart
      • 22303 MODX Staff
      • 10,725 Posts
      Bert:

      First, you are not creating partitions by creating a hierarchy in the key name. This is using the 'default' cache provider (or partition) to store these cache entries. Each partition is a separate derivative instance of the xPDOCache class, e.g. xPDOFileCache, xPDOMemCache, xPDOAPCCache, etc. that has a key and a configuration.

      That said, I recommend getting a cache partition first, and calling delete() directly on it. In your attempt, you are not specifying the partition to remove that cache entry from, so it is attempting to delete it from the 'default' cache partition. You can provide the partition details in the modCacheManager->delete() call, or, as I said, get the specific provider first and work directly with it:

      Here is an example of creating a custom partition and deleting a key within it:

      /** @var modX $modx */
      $cacheKey = $modx->getOption('cache_customcmp_key', $scriptProperties, 'customcmp');
      $cacheOptions = array(
          xPDO::OPT_CACHE_HANDLER => $modx->getOption("cache_{$cacheKey}_handler", $scriptProperties, $modx->getOption(xPDO::OPT_CACHE_HANDLER)),
          xPDO::OPT_CACHE_EXPIRES => $modx->getOption("cache_{$cacheKey}_expires", $scriptProperties, $modx->getOption(xPDO::OPT_CACHE_EXPIRES)),
      );
      /** @var xPDOCache $customCache */
      $customCache = $modx->cacheManager->getCacheProvider($cacheKey, $cacheOptions);
       
      if ($customCache) {
          $contextCache->delete('noticeboard/list/' . $item->get('id') . '.detail');
      } else {
          $modx->log(modX::LOG_LEVEL_ERROR, "Could not get custom cache partition with key {$cacheKey}");
      }
        • 3186 ☆ A M B ☆
        • 279 Posts
        Wow! That looks cool :p

        I just (a minute ago) found that given a second parameter to the delete() method will work too.. So I do now like:

        $modx->cacheManager->delete('noticeboard/'.$item->get('id').'.detail', array(xPDO::OPT_CACHE_KEY => 'customcmps'));


        I do understand a bit of your custom partitions, will look over it to implement it the way it should be ;-)
          MODX Ambassador (NL) & Professional MODX developer
          Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
          MODX e-commerce solution SimpleCart
          • 22303 MODX Staff
          • 10,725 Posts
          FYI, one quirk. If you pass just the xPDO::OPT_CACHE_KEY and the provider has not yet been initialized in that request, it will look for xPDO::OPT_CACHE_HANDLER on it's own, first looking for a Setting prefixed with the xPDO::OPT_CACHE_KEY, e.g. customcmps_cache_handler, falling back to the default cache_handler. You can avoid this by specifying the xPDO::OPT_CACHE_HANDLER (and any other important cache options for the partition) in the options array you pass, either to get the specific cache provider or in the shortcut calls from modCacheManager. This could be important, especially if you want to have custom xPDO::OPT_CACHE_EXPIRES or any other options set on the provider when it is initialized during a request. Failure to specify ALL of the specific options you want to override the defaults could result in unexpected behavior in your custom partition.
            • 3186 ☆ A M B ☆
            • 279 Posts
            Thanks Jason!
            I appreciate your explanation.. In order to keep my work "MODX compatible" I will definitely look over it to implement it in my project, but my solution works like a charm for now ;-) Thanks!
              MODX Ambassador (NL) & Professional MODX developer
              Follow me on Twitter | Visit my page on Facebook | View my code on Github | View my script posts
              MODX e-commerce solution SimpleCart