views:

177

answers:

2

Hi,

What kinds of things should I avoid if I want to support PHP OpCode Caches? Are static calls evil? What about __autoload?

+5  A: 

For every PHP-based web-application I've worked on for the past 3 years and a half, I've always used APC as an opcode cache, on all the servers I'm using...

... And I've never had to take of anything "special" while developping : in every case, using APC or not has been transparent, the only difference being about performances.

I've never had any problem with static calls, nor autoloading, for instance -- nor with anything else (And I've worked with a couple of different Frameworks and OSS Applications)

Still, one good habit : if you plan to use APC on your production server, also use it on your development machines, just in case -- but enable the apc.stat option on those, so your life is not complicated by the opcode caching mecanism.

Pascal MARTIN
Interesting. One question: if I modify a PHP file through PHP, will my OpCode Caches invalidate or will they still continue using the out-dated PHP file?
rFactor
@Tower : with APC (it's the only opcode cache I'm using), it depends on the value of the `apc.stat` configuration directive : if it's set to `1`, modifications will be seen immediatly ;; if it's set to `0`, modifications won't be seen unless you empty the opcode cache (often done by restarting Apache) ;; On my development and staging machines, I set `apc.stat=1` ; on my production servers, I generally use `apc.stat=0` as the code is not updated often ;; of course, setting it to `1` has a (not that big either, but not too small) cost -- up to you to determine the value you should use ^^
Pascal MARTIN
Is there a way to manually clear the cache via PHP? I'm planning on having an auto-update feature, that will update PHP files - I'm wondering if I could use apc.stat=0 and clear the cache when the files are updated?
rFactor
`apc_clear_cache()` (see http://php.net/manual/en/function.apc-clear-cache.php ) should do the trick -- but, on highly loaded servers, I've sometimes seen the server **really** suffer when doing this -- so, what I generally do is an Apache "graceful" restart, to avoid crashing the server when it's under high load (but I'm doing it from the command-line, which is not quite what you want to do... )
Pascal MARTIN
Interesting. Now I have no clues on what to do. Maybe I'll just take the easy way and use apc.stat=1...? Or is there a way to remove only particular files from the cache? I don't think so based on the documentation.
rFactor
Using `apc.stat=1` might be perfectly fine ;; you cannot delete the opcode cache for one file, but you can re-compile it, using `apc_compile_file` (see http://www.php.net/manual/en/function.apc-compile-file.php ) -- I've never tried this, might it might do the trick ;-)
Pascal MARTIN
+1  A: 

An opcode cache is made to cache the compiled version of the script. The Zend Engine under the hood always compiles PHP scripts to faster opcodes before running the script, and it is these opcodes the cache will save. You script will therefore behave in exactly the same way as it should without the cache, only be faster to begin running.

The cache engine usually look in the timestamp (modification time, or mtime) of the PHP file. APC can be configured to look up the modification time on every request (the default), but it could also be configured to NOT check for modification time and in that case you have to manually clear the cache to take up changes. See this setting to the APC cache:

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

Emil Vikström