How to check if APC opcode cache is working fine in PHP?

Lina picture Lina · Feb 16, 2011 · Viewed 48.9k times · Source

I am using PHP with APC cache enabled:

apc.cache_by_default => On
apc.enabled => On
apc.ttl => 7200

Now how can I know if it is using the opcode cache 100%.

For example, let us say that I have this PHP file:

<?php
echo "Hi there";
?>

Now after running this file, let us change it to echo "Bye there";

Shouldn't it echo "Hi there" since the TTL of 7200 seconds is not over yet? Am I right? If so, why does it echo "Bye there"? And if I am wrong how can I force it to use the opcode cache even after changing the file?

Answer

Simon East picture Simon East · Jul 13, 2013

The simplest way that I could find to tell whether APC is working was to create a new PHP file containing this code...

<pre><?php
print_r(apc_cache_info());

It dumps the contents of apc_cache_info() to the screen (be careful, on a large, live site this could be lots of data!).

Every time you reload this PHP file, you should see num_hits increase, meaning that the opcode cache was used. A miss indicates that APC had to recompile the file from source (usually done on every change).


For a nicer interface to this information you can use the apc.php file that comes with APC. I copied this to my website directory using this console command (your folder locations may differ)...

cp /usr/share/doc/php-apc/apc.php /usr/share/nginx/html/apc-stats.php

Running this file in your browser gives you nice colours and graphs!

See this link for further info:
http://www.electrictoolbox.com/apc-php-cache-information/