We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33968
    • 863 Posts
    I'm using modCacheManager to cache and retrieve 'views' for my custom components.

    The next step is to ensure the cache is refreshed when an item is edited from the manager. Calling $modx->cacheManager->refresh(); clears the whole cache which is far from ideal.

    The documentation is a bit sparse on this - how can I refresh a specific cache item, targeted by its cache key? Similar to the way the cache is refreshed when a resource is edited in the manager.

    The following doesn't work but it might give an idea of what I'm trying to do:
    // this is how my cache key is generated:
    $cacheKey = $modx->resource->getCacheKey() . '/' . $itemId;
    
    // this is how I want to refresh it:
    $modx->cacheManager->refresh(array(
        'resource' => array('key' => $cacheKey),
    ));
    

    For example, custom item of id '3', displayed using a view resource of id '12', ends up here:
    core/cache/resource/web/resources/12/3.cache.php
      • 28215
      • 4,149 Posts
      To set:
      $cacheKey = $modx->resource->getCacheKey().'/myextra/'.$itemId;
      $modx->cacheManager->set($cacheKey,$data);
      


      To get:
      $cacheKey = $modx->resource->getCacheKey().'/myextra/'.$itemId;
      $data = $modx->cacheManager->get($cacheKey);
      


      To remove (refresh):
      $cacheKey = $modx->resource->getCacheKey().'/myextra/'.$itemId;
      $modx->cacheManager->delete($cacheKey);
      

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • The MODX cache is partitioned so that specific sections can be cleared in total. Not all cache implementations provide a way to remove specific keys within the cache efficiently, so in general, if you want to clear things manually, and not when the context cache or the resource cache is cleared (for instance), you should use a custom partition by specifying an existing cache partition (identified by xPDO::OPT_CACHE_KEY):

        <?php
        $cacheOptions = array(
            xPDO::OPT_CACHE_KEY => 'myCache',
            xPDO::OPT_CACHE_HANDLER => 'cache.xPDOAPCCache',
        );
         
        $modx->cacheManager->set('testdata', 'My cached data.', 0, $cacheOptions);
         
        echo $modx->cacheManager->get('testdata', $cacheOptions);
        // outputs: My cached data.
         
        $modx->cacheManager->delete('testdata', $cacheOptions);


        See http://rtfm.modx.com/display/xPDO20/Caching#Caching-UtilizingSpecificCachePartitions for additional examples of using a custom partition.

        That said, If you want the cache to be cleared when the resource cache is flushed, then use the resource partition. Here is the code used by the getCache Add-On, which uses the resource partition by default but allows overrides to all the caching options:

        <?php
        if (empty($cacheKey)) $cacheKey = $modx->getOption('cache_resource_key', null, 'resource');
        if (empty($cacheHandler)) $cacheHandler = $modx->getOption('cache_resource_handler', null, $modx->getOption(xPDO::OPT_CACHE_HANDLER, null, 'xPDOFileCache'));
        if (!isset($cacheExpires)) $cacheExpires = (integer) $modx->getOption('cache_resource_expires', null, $modx->getOption(xPDO::OPT_CACHE_EXPIRES, null, 0));
        if (empty($cacheElementKey)) $cacheElementKey = $modx->resource->getCacheKey() . '/' . md5($modx->toJSON($properties) . implode('', $modx->request->getParameters()));
        $cacheOptions = array(
            xPDO::OPT_CACHE_KEY => $cacheKey,
            xPDO::OPT_CACHE_HANDLER => $cacheHandler,
            xPDO::OPT_CACHE_EXPIRES => $cacheExpires,
        );
        
        $cached = $modx->cacheManager->get($cacheElementKey, $cacheOptions);
        


        Otherwise, you will have to use a plugin or other scripted approach to clear your custom provider when users choose Clear Cache in the MODX Manager. FWIW, the above example also adds any custom request parameters to the cache key for instances where you need to cache request specific instances of something.
          • 33968
          • 863 Posts
          Two great responses - thanks guys smiley

          I hadn't realised $modx->cacheManager->delete(); existed but will have a go at that now.

          Also, Jason the code I'm using is actually a modification of getCache and the custom request functionality you pointed out is exactly what I need.

          Thanks again!