views:

4700

answers:

4

I need to clear all APC cache entries when I deploy a new version of the site. APC.php has a button for clearing all opcode caches, but I don't see buttons for clearing all User Entries, or all System Entries, or all Per-Directory Entries.

Is it possible to clear all cache entries via the command-line, or some other way?

+5  A: 

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.

Travis Beale
I discovered that to do this via command-line you need to go into apc.ini and set: apc.enable_cli=1
lo_fye
lo_fye: Does that actually work? In my experience, I found that APC CLI was totally separate from apache's APC cache -- and rightfully so, since any CLI process runs in a completely separate process from Apache.
Frank Farmer
+5  A: 

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

apc_clear_cache('opcode');

Also, you can't do this in cli-mode to clear the opcode cache for mod_php or fastcgi. See comment.

ColinM
I should add, if you are running mod_php and want to clear the cache via cli-mode php, you can't really do this since the two are running in different environments. My solution was to have the cli mode php call itself over http using file_get_contents. Ugly, but it works.
ColinM
A: 

We had a problem with APC and symlinks to symlinks to files -- it seems to ignore changes in files itself. Somehow performing touch on the file itself helped. I can not tell what's the difference between modifing a file and touching it, but somehow it was necessary...

jakub.lopuszanski
A: 

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'); //my code does something specific to Symfony here
}

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']);
jeremy