问题
I would like to clear my frontend application's cache from an action in my backend application.
How can I achieve this?
回答1:
I believe the proper way to do this in symfony 1.2 is as follows:
sfContext::switchTo('frontend'); //switch to the environment you wish to clear
sfContext::getInstance()->getViewCacheManager()->getCache()->clean(sfCache::ALL);
sfContext::switchTo('backend'); //switch back to the environment you started from
回答2:
This works for me. It removes all cached files from the given directory:
$cache_dir = sfConfig::get('sf_cache_dir').'/'.$app.'/'.$env.'/';
$cache = new sfFileCache(array('cache_dir' => $cache_dir));
$cache->clean();
回答3:
If anyone is looking for clearing one cache item (one page):
sfContext::switchTo('frontend');
sfContext::getInstance()->getViewCacheManager()->remove("module/action?¶m1=value1¶m2=value2","THE-DOMAIN-OF-YOUR-FRONTEND-APPLICATION-IF-U-USE-IT-IN-CACHE-KEYS");
sfContext::switchTo('backend');
回答4:
I don't think there is no "clean" way to do the job, as different apps are treated as quite separated environments in symfony. Obviously the job may be done in a less or more dirty way, choose your way to remove any file in the cache/ dir, run the phing task clear-cache (cc) etc ...
you can simply run rm -rf cache/*, but you could break some client request. The simpler thing could be to run symfony cc via passthru() or exec()
回答5:
You can create instance of sfTask and run it like this (in sf 1.2):
$task = new sfCacheClearTask(sfContext::getInstance()->getEventDispatcher(), new sfFormatter());
$arguments = array();
// type can be one of: i18n, routing, template, module, config
$options = array(
'frontend' => 'app',
'routing' => 'type',
'prod' => 'env',
);
$task->run($arguments, $options);
For all possible arguments and options see source code of appropriate sfTask...
来源:https://stackoverflow.com/questions/1327645/clearing-symfony-cache-for-another-application