10

I am developing a website in symfony framework. In my cache folder a huge cache is stored. I want to disable cache permanently.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
Karan Kumar
  • 287
  • 2
  • 5
  • 12

6 Answers6

16

While I advise against disabling the cache on a production system, you can disable the twig templating engine cache, by editing and adding to your config.yml file

twig:
    cache: false
Diego Ferri
  • 2,657
  • 2
  • 27
  • 35
  • 1
    Thanks for this answer @DiegoFerri. I am using Symfony 2.7.7 and in dev mode it still caching even if I `//$kernel->loadClassCache();` comment out my cache in app_dev.php What I had to do is add in my `config_dev.yml` your snippet and all done!! – George Mylonas Jan 20 '16 at 11:54
8

The class cache in Symfony2 can be disabled in you app.php or app_dev.php file:

$loader = require_once __DIR__.'/../app/autoload.php';
Debug::enable();

$kernel = new AppKernel('dev', true);
#$kernel->loadClassCache();  // <-- comment out this line
$request = Request::createFromGlobals();

Symfony2 - Disabling the Bootstrap File and Class Caching

StampyCode
  • 7,218
  • 3
  • 28
  • 44
6

I was having caching issues even when using app_dev.php. I would change a route but it wouldn't update when I tried accessing it via a browser.

I tried commenting out the anything that had cache in it (as stated above). My AppKernel('dev', true) was set to true. Nothing worked.

If I ran the console cache:clear it would fix it, but the next routing change would break again. I had to run cache:clear with every save, which was ridiculous.

My issue turned out that because I was working remotely over SFTP, PHP Storm (my editor) was "preserving timestamp" in its deployment configuration. Once I changed that configuration the issues went away. Apparently there is some caching going on that is looking at the file timestamps, even in the dev environment.

2

I think you can't disable "permanently cache", since Symfony applications use some cached files in order to run faster (or simply to run). Examples of this are the files that contains the dependency injection container (appProdProjectContainer.php).

You can disable some types of cache like Twig cache (as Diego Ferri said before) or Http Cache (unwrapping AppKernel with AppCache in app.php) or even Doctrine cache (in config.yml).

However I would not recommend this. The more you cache the app, the faster your app will be.

  • correct - symfony relies on caching - some cache (like the classesArray) is rebuild every request, when it is deleted. – cklm Oct 22 '16 at 08:36
0

When you are working in a dev environment state, the cache is disabled anyway - I'm assuming you only want to have it disabled within development, so use the /app_dev.php file to make sure nothing is cached.

Alternatively you can empty the cache periodically on the command line using

php app/console cache:clear

You can see all the different parameters here: http://symfony.com/doc/current/cookbook/console/usage.html

Michael Emerson
  • 1,774
  • 4
  • 31
  • 71
0

This is from symfony documentation:

https://symfony.com/doc/current/bundles/override.html

If you add a template in a new location, you may need to clear your cache (php bin/console cache:clear), even if you are in debug mode.

That means that the rules how and when the cache is cleared is not even clearly defined in the documentation.

Sometimes you want to change and debug files in vendor folder and after that even this command will not help you:

bin/console cache:clear

The only thing that will help you is to delete all content inside /var/cache/dev folder

A dirty hack for your local environment which you can use to clear cache after each request is:

<?php

namespace App;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

    public function terminate(Request $request, Response $response)
    {
        system("rm -rf ".escapeshellarg('../var/cache/dev'));

        return parent::terminate($request, $response);
    }
}
fico7489
  • 7,931
  • 7
  • 55
  • 89
  • I've never equally hated and loved a solution more in my life. IF one is to go down this route (which is not particularly recommended) It would probably be best to only execute this when in a "debug" or "dev" `APP_ENV` or possibly some other env var dedicated to turning cache on/off to avoid possibly doing this in a production env. – ffgpga08 Jan 27 '23 at 21:44
  • yes of course some of the control in the .env should be added, this is just an example how do do it roughly – fico7489 Jan 27 '23 at 21:51