Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "cache filecache folder"

Redirect cache output depending on module/action

In my current application I found that I often needed to perform a clear-cache to get newly published changes to work on the production site, but in doing this I would clear a lot of valuable data from the template cache.

One solution would have been to turn off cacheing in the appropriate cache.yml file, and then to implement it manually, which would have been fairly easy. However I really like the transparent cacheing feature of symfony, so wrote this extension to sfFileCache to redirect cache output to a safe place, providing that certain criteria are met.

Feedback welcome here.

/**
 * Class to modify location of cache dynamically, thus protecting important files against clear-cache
 *
 * @version 19 Feb 2007
 * 
 * Notes: to configure, place this class in the appropriate lib folder, then in the app settings,
 * enter the following under the all category:
 *
 * all:
 *   safe_cache:
 *     folder:                       altcache
 *     activate:                     [[module1, action1], [module2, action2] ...]
 *
 * Where "altcache" is the name of the alternative cache location, relative to the project root,
 * and the module/action pairs are the actions that the safe cache is to be switched on for.
 *
 * Finally this class needs to be installed in factories.yml:
 *
 * all:
 *   view_cache:
 *     class: SafeFileCache
 *
 * Note that intercepting the constructor was not particularly feasible, as it is called very
 * early in the bootstrapping process, before the context instance is available. This means that
 * getModuleName and getActionName are not be available to test against, leaving us to test the
 * URL, which not be as clean.
 */
class SafeFileCache extends sfFileCache
{
  public function get($id, $namespace = self::DEFAULT_NAMESPACE, $doNotTestCacheValidity = false)
  {
    $isDiff = $this->isDifferentCache();
    if ($isDiff)
    {
      $save = $this->cacheDir;
      $this->cacheDir = $this->getCachePath();
    }
 
    $result = parent::get($id, $namespace, $doNotTestCacheValidity);
 
    if ($isDiff)
    {
      $this->cacheDir = $save;
    }
 
    return $result;
  }
 
  public function has($id, $namespace = self::DEFAULT_NAMESPACE, $doNotTestCacheValidity = false)
  {
    $isDiff = $this->isDifferentCache();
    if ($isDiff)
    {
      $save = $this->cacheDir;
      $this->cacheDir = $this->getCachePath();
    }
 
    $result = parent::has($id, $namespace, $doNotTestCacheValidity);
 
    if ($isDiff)
    {
      $this->cacheDir = $save;
    }
 
    return $result;
  }
 
  public function set($id, $namespace = self::DEFAULT_NAMESPACE, $data)
  {
    $isDiff = $this->isDifferentCache();
    if ($isDiff)
    {
      $save = $this->cacheDir;
      $this->cacheDir = $this->getCachePath();
    }
 
    $result = parent::set($id, $namespace, $data);
 
    if ($isDiff)
    {
      $this->cacheDir = $save;
    }
 
    return $result;
  }
 
  public function remove($id, $namespace = self::DEFAULT_NAMESPACE)
  {
    $isDiff = $this->isDifferentCache();
    if ($isDiff)
    {
      $save = $this->cacheDir;
      $this->cacheDir = $this->getCachePath();
    }
 
    $result = parent::remove($id, $namespace);
 
    if ($isDiff)
    {
      $this->cacheDir = $save;
    }
 
    return $result;
  }
 
  private function isDifferentCache()
  {
    $context = sfContext::getInstance();
    $strModule = $context->getModuleName();
    $strAction = $context->getActionName();
 
    $run = false;
    $allowed = sfConfig::get('app_safe_cache_activate');
    if (is_array($allowed))
    {
      foreach($allowed as $allow)
      {
        if (is_array($allow) && (count($allow) == 2))
        {
          list($mod, $act) = $allow;
          if (($mod == $strModule) && ($act == $strAction))
          {
            $run = true;
            break;
          }
        }
      }
    }
 
    return ($run);
  }
 
  private function getCachePath()
  {
    $sysDir = sfConfig::get('sf_root_dir');
    $oldDir = $sysDir . DIRECTORY_SEPARATOR . sfConfig::get('sf_cache_dir_name');
    $newDir = $sysDir . DIRECTORY_SEPARATOR . sfConfig::get('app_safe_cache_folder');
    $cacheDir = $newDir . str_replace($oldDir, '', $this->cacheDir);
 
    return $cacheDir;
  }
}
by halfer on 2007-02-19, tagged cache  clear  filecache  folder