简体   繁体   English

以编程方式清除symfony 2上的缓存

[英]Programmatically clear cache on symfony 2

I'm trying to empty the Symfony 2 cache through my application just as if I was doing php app/console cache:clear . 我试图通过我的应用程序清空Symfony 2缓存,就好像我在做php app/console cache:clear

I've tried many different solutions, such as: 我尝试了很多不同的解决方案,例如:

//solution 1
$process = new Process('php '.__DIR__.'/../../../../../app/console cache:clear');
$process->setTimeout(3600);
$process->run();

//solution 2
$input = new StringInput(null);
$output = new NullConfigurationOutput();
$command = new CacheClearCommand();
$command->setContainer($container);
$command->run($input, $output);

//solution 3    
$container->get('cache_clearer')->clear($container->getParameter('kernel.cache_dir'));

I've also tried to rm -Rf the cache folder. 我也试过rm -Rf缓存文件夹。

None of them is fully working as i'm getting many errors. 他们都没有完全正常工作,因为我遇到了很多错误。 The following error is the one i'm getting on solution 2 (which seemed to be the best): 以下错误是我正在解决方案2(这似乎是最好的):

Failed to start the session: already started by PHP ($_SESSION is set). 无法启动会话:已由PHP启动(已设置$ _SESSION)。

Seems like Symfony is trying to rebuild the page or reboot the application during the render process. 似乎Symfony正在尝试重建页面或在渲染过程中重新启动应用程序。

How could someone clear the cache via some php code? 怎么会有人通过一些PHP代码清除缓存?

EDIT : I'm actually trying to regenerate the cache because of my routing system. 编辑:由于我的路由系统,我实际上正在尝试重新生成缓存。 I've made a bundle which take a symfony 2 route and add the possibility to rewrite it for SEO purpose (example : i have a front_category route which point to /category, with this bundle i can rewrite it to category-seo-optimized.html). 我已经制作了一个包含symfony 2路径的捆绑包并添加了为SEO目的重写它的可能性(例如:我有一个指向/ category的front_category路由,使用此捆绑包我可以将其重写为category-seo-optimized。 HTML)。

If i'm on a route which needs particular parameter, such as an ID, i need to make a special route for it in order to rewrite it. 如果我在需要特定参数的路线上,例如ID,我需要为它做一个特殊的路线才能重写它。 For example, for /category/3 i need to create a route which is named category_3 and.. in order to let symfony knows i've changed the routing system, i had to clear the cache. 例如,对于/ category / 3我需要创建一个名为category_3和..的路由,为了让symfony知道我已经改变了路由系统,我必须清除缓存。

Exactly the same problem that I just had. 我刚刚遇到的问题完全相同。 Simplest solution for me was: 对我来说最简单的解决方案是:

use Symfony\Component\Filesystem\Filesystem;

and in controller: 在控制器中:

$fs = new Filesystem();
$fs->remove($this->container->getParameter('kernel.cache_dir'));

This will delete the cache directory for the current environment. 这将删除当前环境的缓存目录。

A similar question is already answered here: Symfony 2.4 execute Command from Controller - answered Mar 20 at 15:37 pomaxa. 这里已经回答了类似的问题: Symfony 2.4执行来自控制器的命令 - 3月20日15:37 pomaxa回答。

I've tested and extended pomaxa's code : 我已经测试并扩展了pomaxa的代码

Controller code 控制器代码

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

$command = $this->container->get('yourservice.cache.clear');
//$dynamicEnvMode = $this->container->getParameter('kernel.environment')
$staticEnvMode = 'dev'; // to use develop mode
$staticEnvMode = 'prod --no-debug'; // to use production mode
$input = new ArgvInput(array('--env=' . $staticEnvMode ));
$output = new ConsoleOutput();
$command->run($input, $output);

I used static environment mode and I tried to use CacheClearCommand object instead of service, but it haven't worked. 我使用静态环境模式,我尝试使用CacheClearCommand对象而不是服务,但它没有工作。 Cache clear operation can use long time to load, so using a service might do it better. 缓存清除操作可能需要很长时间才能加载,因此使用服务可能会做得更好。

Best way would be solve your problem without cache clear. 最好的方法是解决您的问题,没有缓存清除。 Find the fast changing entities,actions or anything you need, and disable caching for them. 查找快速变化的实体,操作或您需要的任何内容,并禁用它们的缓存。 If you post a detailed question (and put the link in comment) from, why you need ignore cache and where, then I could help you to find an other way. 如果你发布一个详细的问题(并在评论中添加链接),为什么你需要忽略缓存和哪里,那么我可以帮助你找到另一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM