Drupal website specialists.

Simple cache management class for Symfony

Posted 21/July/2008 by neubreed

<?php
class nbCacheManager {
 
    static $cache_dirs = array();
 
    /**
    * @desc Clears partial cache files
    * @access public
    * @param mixed - leave blank for ALL partials
    * @param string - application
    * @return void
    * @author Ryan Johnson
    * @version 
    */
    static public function clearPartialCache($partial_path = array(), $app = 'public')
    {
        $partial_paths = is_array($partial_path) ? 
                                   $partial_path : array($partial_path);
 
        foreach($partial_paths as $path)
        {
            $path = str_replace('/', '/_', $path);
            self::$cache_dirs[] = sprintf('/%s/*/template/*/all/sf_cache_partial/%s', 
                                                          $app, 
                                                          $path
                                              );
        }
        self::doClearCache();
    }
 
    /**
    * @desc Clears global cache
    * @access public
    * @param string - leave blank for ALL partials
    * @param string - application
    * @return void
    * @author Ryan Johnson
    * @version 
    */
    static public function clearCache($path = array(), $app ='public')
    {
        $paths = is_array($path) ? $path : array($path);
 
        foreach($paths as $path)
        {
            self::$cache_dirs[] = sprintf('/%s/*/template/*/all/%s', $app, $path);
        }
        self::doClearCache();
    }
    /**
    * @desc executes the clear
    * @access public
    * @param void
    * @return void
    * @author Ryan Johnson
    * @version 
    */
    static private function doClearCache()
    {
        foreach(self::$cache_dirs as $cache_dir)
        {
            sfToolkit::clearGlob( sfConfig::get('sf_root_cache_dir') );
        }
    }
 
    /**
    * @desc clears all template cache
    * @access public
    * @param string - application
    * @return void
    * @author Ryan Johnson
    * @version 
    */
    static public function clearTemplateCache($app ='public')
    {
        self::$cache_dirs = array(sprintf('/%s/*/template/*/all', $app));
        self::doClearCache();
    }
 
}
?>

I've used in at the end of the admin generator save method, so that everytime a record is saved in the backend the template cache is cleared

  protected function saveMyModule($my_module)
  {
       $my_module->save();
       nbCacheManager::clearTemplateCache();
  }