从documentation(顺便说一下,1.4版本在哪里?!),我尝试使用此代码从后端应用程序中删除前端缓存页面:
$frontend_cache_dir = sfConfig::get('sf_cache_dir').DIRECTORY_SEPARATOR.'frontend'.DIRECTORY_SEPARATOR.sfConfig::get('sf_environment').DIRECTORY_SEPARATOR.'template';
$cache = new sfFileCache(array('cache_dir' => $frontend_cache_dir));
$cache->removePattern($uri);
它什么都不做。所以我在source code挖了一下,这里是removePattern
函数:
public function removePattern($pattern)
{
if (false !== strpos($pattern, '**'))
{
$pattern = str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION;
$regexp = self::patternToRegexp($pattern);
$paths = array();
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->getOption('cache_dir'))) as $path)
{
if (preg_match($regexp, str_replace($this->getOption('cache_dir').DIRECTORY_SEPARATOR, '', $path)))
{
$paths[] = $path;
}
}
}
else
{
$paths = glob($this->getOption('cache_dir').DIRECTORY_SEPARATOR.str_replace(sfCache::SEPARATOR, DIRECTORY_SEPARATOR, $pattern).self::EXTENSION);
}
foreach ($paths as $path)
{
if (is_dir($path))
{
sfToolkit::clearDirectory($path);
}
else
{
@unlink($path);
}
}
}
当然,它无法工作,因为它不会生成缓存键,就像缓存管理器的remove
函数一样。
$path
变量应该是这样的:/home/.../cache/frontend/dev/template/localhost/all/module/action/key1/value1/key2/value2.cache
我最终得到了这个:/home/.../cache/frontend/dev/template/module/action?key1=value1&key2=value2.cache
此$path
变量不包含主机名和变量标头
此外,所有参数(key1=value1&key2=value2
)都未转换为/key1/value1/key2/value2
如何在不破坏Symfony核心的情况下解决这个问题?
答案 0 :(得分:0)
我修复了这个切换上下文。文档肯定是错误的。
sfContext::switchTo('frontend');
sfContext::getInstance()->getViewCacheManager()->remove($uri);
sfContext::switchTo('backend');
感谢jeremy