Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

opcode

apc

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?

like image 519
Lina Avatar asked Feb 16 '11 14:02

Lina


People also ask

What is PHP opcode caching?

OpCode Caches are a performance enhancing extension for PHP. They do this by injecting themselves into the execution life-cycle of PHP and caching the results of the compilation phase for later reuse. It is not uncommon to see a 3x performance increase just by enabling an OpCode cache.

What is APC user cache?

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.


1 Answers

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/

like image 114
Simon East Avatar answered Sep 27 '22 03:09

Simon East