Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear APC cache entries?

People also ask

How to clear apc cache?

Clear APC Cache php if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0. 0.1', '::1', '123.123. 123.123'))) { apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode'); echo json_encode(array('success' => true)); } else { die('No valid IP'); } ?>

How to clear PHP cache?

The clearstatcache() function clears the file status cache. PHP caches data for some functions for better performance. If a file is to be checked several times in a script, you probably want to avoid caching to get correct results. To do this, use the clearstatcache() function.

How does APC cache work?

The goal of APC is bypass the steps from 1 to 4, caching in a shared memory segment the opcodes generated and then copies them into the execution process so Zend can actually execute the opcodes. 1. During module init time ( MINIT), APC uses mmap to map file into shared memory.


You can use the PHP function apc_clear_cache.

Calling apc_clear_cache() will clear the system cache and calling apc_clear_cache('user') will clear the user cache.


I don't believe any of these answers actually work for clearing the APC cache from the command line. As Frank Farmer commented above, the CLI runs in a process separate from Apache.

My solution for clearing from the command line was to write a script that copies an APC clearing script to the web directory and accesses it and then deletes it. The script is restricted to being accessed from the localhost.

  1. apc_clear.php

    This is the file that the script copies to the web directory, accesses, and deletes.

    <?php
    if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')))
    {
      apc_clear_cache();
      apc_clear_cache('user');
      apc_clear_cache('opcode');
      echo json_encode(array('success' => true));
    }
    else
    {
      die('SUPER TOP SECRET');
    }
    
  2. Cache clearing script

    This script copies apc_clear.php to the web directory, accesses it, then deletes it. This is based off of a Symfony task. In the Symfony version, calls are made to the Symfony form of copy and unlink, which handle errors. You may want to add checks that they succeed.

    copy($apcPaths['data'], $apcPaths['web']); //'data' is a non web accessable directory
    
    $url = 'http://localhost/apc_clear.php'; //use domain name as necessary
    $result = json_decode(file_get_contents($url));
    
    if (isset($result['success']) && $result['success'])
    {
      //handle success
    }
    else
    {
      //handle failure
    }
    
    unlink($apcPaths['web']);
    

I know it's not for everyone but: why not to do a graceful Apache restart?

For e.g. in case of Centos/RedHat Linux:

sudo service httpd graceful

Ubuntu:

sudo service apache2 graceful

This is not stated in the documentation, but to clear the opcode cache you must do:

apc_clear_cache('opcode');

EDIT: This seems to only apply to some older versions of APC..

No matter what version you are using you can't clear mod_php or fastcgi APC cache from a php cli script since the cli script will run from a different process as mod_php or fastcgi. You must call apc_clear_cache() from within the process (or child process) which you want to clear the cache for. Using curl to run a simple php script is one such approach.


If you are running on an NGINX / PHP-FPM stack, your best bet is to probably just reload php-fpm

service php-fpm reload (or whatever your reload command may be on your system)


If you want to clear apc cache in command : (use sudo if you need it)

APCu

php -r "apcu_clear_cache();" 

APC

php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"