3

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 Answers 3

10

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

Sign up to request clarification or add additional context in comments.

8 Comments

So the cache.php would be opcode compiled? But apc_store would be still better and faster?
Yes, the file it will be opcode compiled. apc_store does something different, it stores structures. These are 2 different things for 2 different needs. I'll update my answer with more details
I don't like the first sentence: APC does not compile op-codes, thats what the engine is for. APC provides a byte-code (opcode-/system-cache). However, it's true, that it is not the same, as the variable-cache (user-cache), but at the end they behave very similar, except that the first one is used by the extension itself (implict) and the second one is used by userland code (explicit). As a sidenote: Datastructures cannot "calculate", they just exists ;) So your example is misleading too.
@KingCrunch: you are correct, very bad terminology on my side, I'll update my answer
But you store the output in the cache.php, that's why it is called "cache.php"
|
2

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.

10 Comments

You can disable the stat-call with php.net/apc.configuration.php#ini.apc.stat
That is true, but the cache must then be manually flushed in order to reflect changes made to the file. There is a tendency for people to forget this when making updates, particularly when there are lots of files being changed.
This is, what deploy-scripts are invented for ;)
A person still has to write the deploy script, usually the same person who forgets about the config file being changed :)
And the same person, that forgot to switch the lights off ... We can imagine many cases, where everything can go horrible wrong... But to be realistic: a) The same dude, that set stat=0 changes the deploy script and b) even if not, someone will notice it and change the script. After this it will clear the cache for the next 150 years. The one time, where someone forgot it, will also not lead to a black hole under your servers. I would clean the cache anyway (as essential part of every deployment), independent whether stat is off, or not ;) And not only APC
|
1

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.

3 Comments

That's interesting, I wouldn't expect this to be faster than apc_fetch/apc_store
I don't think it's faster either, but I think the difference is soooo much negligable ;)
I originally asked this question because I too think that the difference is negligible, but using files: (1) you don't write an additional code; (2) even if you don't have APC it is still cached

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.