3
votes

Let's say I cache data in a PHP file in PHP array like this:

/cache.php

<?php return (object) array(
    'key' => 'value',
);

And I include the cache file like this:

<?php
$cache = include 'cache.php';

Now, the question is will the cache file be automatically cached by APC in the memory? I mean as a typical opcode cache, as all .php files.

If I store the data differently for example in JSON format (cache.json), the data will not be automatically cached by APC?

Would apc_store be faster/preferable?

3

3 Answers

10
votes

Don't mix APC's caching abilities with its ability to optimize intermediate code and cache compiled code. APC provides 2 different things:

  1. It gives a handy method of caching data structures (objects, arrays etc), so that you can store/get them with apc_store and apc_fetch
  2. It keeps a compiled version of your scripts so that the next time they run, they run faster

Let's see an example for (1): Suppose you have a data structure which takes 1 second to calculate:

function calculate_array() {
    sleep(1);
    return array('foo' => 'bar');
}
$data = calculate_array();

You can store its output so that you don't have to call the slow calculate_array() again:

function calculate_array() {
    sleep(1);
    return array('foo' => 'bar');
}
if (!apc_exists('key1')) {
    $data = calculate_array();
    apc_store('key1', $data);
} else {
    $data = apc_fetch('key1');
}

which will be considerably faster, much less than the original 1 second.

Now, for (2) above: having APC will not make your program run faster than 1 second, which is the time that calculate_array() needs. However, if your file additionally needed (say) 100 milliseconds to initialize and execute, simply having enabled APC will make it need (approx) 20 millisecond. So you have an 80% increase in initialization/preparation time. This can make quite a difference in production systems, so simply installing APC can have a noticeable positive impact on your script's performance, even if you never explicitly call any of its functions

2
votes

If you are just storing static data (as in your example), it would be preferable to use apc_store.

The reasoning behind this is not so much whether the opcode cache is faster or slower, but the fact you are using include to fetch static data into scope.

Even with an opcode cache, the file will still be checked for consistency on each execution. PHP will not have to parse the contents, but it will have to check whether the file exists, and that it hasn't changed since the opcode cache was created. Filesystem checks are resource expensive, even if it is only to stat a file.

Therefore, of the two approaches I would use apc_store to remove the filesystem checks completely.

1
votes

Unlike the other answer I would use the array-file-solution (the first one)

<?php return (object) array(
    'key' => 'value',
);

The reason is, that with both solutions you are on the right side, but when you let the caching up to APC itself you don't have to juggle around with the apc_*()-functions. You simply include and use it. When you set

apc.stat = 0

you avoid the stat-calls on every include too. This is useful for production, but remember to clear the system-cache on every deployment.

http://php.net/apc.configuration.php#ini.apc.stat

Oh, not to forget: With the file-approach it works even without APC. Useful for the development setup, where you usually shouldn't use any caching.