tags:

views:

563

answers:

3

I'm currently implementing memcached into my service but what keeps cropping up is the suggestion that I should also implement APC for caching of the actual code.

I have looked through the few tutorials there are, and the PHP documentation as well, but my main question is, how do I implement it on a large scale? PHP documentation talks about storing variables, but it isn't that detailed.

Forgive me for being uneducated in this area but I would like to know where in real sites this is implemented. Do I literally cache everything or only the parts that are used often, such as functions?

Thanks!

+3  A: 

APC is both an opcode cache and a general data cache. The latter works pretty much like memcached, whereas the opcode cache works by caching the parsed php-files, so that they won't have to be parsed on each request. That can generally speed up execution time up quite a bit.

troelskn
Ah, it sounds as if the Opcode cache will be the most useful part to my service. Do you have any further information about implementing APC in an Opcode cache situation?
James
well .. you basically just install the extension and set some config settings in php.ini. If you [download the source code](http://pecl.php.net/package/apc) for the extension from pecl, there is a small web application distributed with it, that you can use to see some statistics. Otherwise, [the manual](http://www.php.net/apc/) is fairly comprehensive.
troelskn
it's really just a matter of setting how much space it will use, and dropping it into the server. For most significant websites, it's a no-brainer to want to have. I've seen a machine go from a a load-avg of 1.0+ to <0.3 when I installed it on a server, because there had been so much redundant compilation going on for every page-load.
Alister Bulman
+3  A: 

As you know PHP is an interpreted language, so everytime a request arrives to the server it need to open all required and included files, parse them and execute them. What APC offers is to skip the require/include and parsing steps (The files still have to be required, but are stored in memory so access is much much faster), so the scripts just have to be executed. On our website, we use a combination of APC and memcached. APC to speed up the above mentioned steps, and memcached to enable fast and distributed storing and accessing of both global variables (precomputed expensive function calls etc that can be shared by multiple clients for a certain amount of time) as well as session variables. This enables us to have multiple front end servers without losing any client state such as login status etc.

When it comes to what you should cache... well, that really depends on your application. If you have a need for multiple frontends somewhere down the line, I would try to go with memcached for such caching and storing, and use APC as an opcode cache.

PatrikAkerstrand
Yea this sounds like the type of system I will most likely use myself. Do you set how long files are stored in the APC cache for, what is the ideal size for an APC cache (10mb, 500mb, etc?) and if changes are made to the actual PHP file, does it re-cache or only after the time period set for caching expires?
James
As default, APC checks the modification time of each required file for a request. Thus, if you make a new deploy, APC will automatically parse and cache the most recent file. The apc-variable to look into here is *apc.stat*. IF you turn this off, you will need to restart the web server to use the new code. This will, however, make APC faster since it doesn't need to check the file system.Memory... I'm not really sure. Again, it depends on your system. If you use it for OpCode caching only, it won't need that much memory.
PatrikAkerstrand
How much psace it requires depends on how big your code is. For a good sized one, I'd start at 64MB. I tend to leave plenty of headroom as well 2.5-3x more than the code actually ends up taking, so I can put another version in beside it. For a busy site, apc.stat=0 is useful, but you can also clear the cache from a function call, rather than restarting Apache.
Alister Bulman
+1  A: 

You don't have to implement the opcode caching features of APC, you just enable them as a php module.

APC cache size and other configuration information is here.

Karsten