How to clear Doctrine APC cache for production?
Asked Answered
M

4

8

I have this issue when I add a column to one of my entities and release it for production I have to restart Apache in order to clear Doctrine metadata APC/APCU cache.

I have tried all commands below but none worked for me:

php -r "apc_clear_cache();"
php -r "apcu_clear_cache();"

sudo php app/console doctrine:cache:clear-metadata
sudo php app/console doctrine:cache:clear-query
sudo php app/console doctrine:cache:clear-result

I get this error message for --env=prod

sudo php app/console doctrine:cache:clear-metadata  --env=prod
sudo php app/console doctrine:cache:clear-query  --env=prod
sudo php app/console doctrine:cache:clear-result --env=prod

 [LogicException]
  Cannot clear APC Cache from Console, its share in the Web server memory and not accessible from the CLI.

The only way I can get it to refresh Doctrine cache is to restart my apache server which can sometimes be an issue.

My cache settings for Doctine in my Symfony project:

doctrine:
    orm:
        metadata_cache_driver: apc
        result_cache_driver: apc
        query_cache_driver: apc
        second_level_cache:
            enabled: true
            log_enabled: false
            region_cache_driver: apc

How can I clear APC cache in this case without restarting Apache each time I release new schema update to production. This is even worse if you have many servers behind a load balancer.

Maggiore answered 11/10, 2016 at 15:53 Comment(0)
Z
8

You must understand that Php running under Apache (or Nginx) is different than the Php running via the command line, they are 2 Linux process that cant communicate.

So, even if you can clear the cache via CLI, this will not affect the php under Apache.

The easiest way it to call apcu_clear_cache inside a Symfony controller, or, you can use the Apache Php socket via the CLI.

I recommend to use a tool like http://gordalina.github.io/cachetool/, which do that perfectly.

Zacarias answered 8/11, 2017 at 19:37 Comment(0)
F
2

Instead of trying to clear it from the console, try doing it from a controller or app.php.

I have this line commented in the app.php:

//apcu_clear_cache ();

When I do need to clear the cache, I just uncommented it and load any page. It works for me.

Fradin answered 11/10, 2016 at 18:49 Comment(1)
Clearing apcu cache on each pageload suppresses its benefitsUseless
H
1

When you run apc_clear_cache(); not from cli but from your app, doctrine cache will be cleared. You can make some button in admin panel, or send curl request to your server after each update to specified file, for example:

http://example.com/apc_cache_clear.php

inside autorize request and clear cache

Hun answered 1/3, 2017 at 14:56 Comment(2)
how about if you had 100s of servers?Maggiore
Pmoubed then you should use Redis servers and Redis cache instead of APC :)Hun
S
0

Take a look at the Smart-Core/AcceleratorCacheBundle Provide a command to clear PHP Accelerator cache from CLI.

This can permit to flush the cache as a cli command as:

php app/console cache:accelerator:clear

The doc include also a Capifony's recipe.

Hope this help

Sacha answered 11/10, 2016 at 19:28 Comment(1)
Preferably I don't want to use another Bundle just for this.Maggiore

© 2022 - 2024 — McMap. All rights reserved.