Search code examples
phpcommand-line-interfaceapc

apc_clear_cache not working from command line in php


I have a php script which clears apc. The script is working fine, when I opening it using browser, but when I am running that file from command line, it is not clearing cache.

I checked for apc.enable_cli setting, and that is also on (check the screenshot). Settings dump

And here is my php-code

<?php

if (isset($argv[1])) {
    $key = $argv[1];

    $info = apc_cache_info("user");
    foreach ($info['cache_list'] as $obj) {
        if (strstr($obj['info'], $key)) {
            apc_delete($obj['info']);
        }
    }

} else {
    apc_clear_cache("user");
}
?>

What am I missing or doing wrong?


Solution

  • You can't clear APC cache from command-line, as you're not hitting the same APC segment of your webserver.

    Note that enable_cli only allows you to use APC in a CLI environment, but creates a segment for your script, and destroy it at the end of the script. It doesn't use the same segment because it doesn't know about your webserver.

    You have two options:

    • call your script through FastCGI (see below)
    • call the webpage with file_get_contents() or the like, using http://

    If you need to access APC data, you can also read my article: https://www.dugwood.com/949904-php5-opcode-caching-and-memory-storage-with-apc-xcache-in-command-line-interface-cli-or-cron.html