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

49,163

Solution 1

I don't think you'll want to do it in production, but you could always use apc_cache_info().

function is_file_cached($file) {
    $info = apc_cache_info();
    foreach ($info['cache_list'] as $cache) {
        if ($cache['filename'] == $file) return true;
    }
    return false;
}

Note that this will iterate over every single file that's cached checking for the specified one, so it's not efficient.

And as far as your specific question, APC will automatically invalidate the cache for a file when it changes. So when you edit the file, APC silently detects this and serves the new file. You can disable this by setting apc.stat = 0.

Solution 2

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/

Solution 3

Normally APC checks if the requested file has been modified since it's been cached. You can control this with apc.stat.

Share:
49,163

Related videos on Youtube

Lina
Author by

Lina

Updated on July 09, 2022

Comments

  • Lina
    Lina almost 2 years

    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?