Alex Rivera | Logout

Using PHP APC cache in CLI mode using dumpfiles

Asked 2012-04-05T13:17:52.097
11

I've recently started using APC cache on our servers. One of the most important parts of our product is a CLI (Cron/scheduled) process, whose performance is critical. Typically the batchjob consists of running some 16-32 processes in parallel for about an hour (they "restart" every few minutes).

By default, using APC cache in CLI is a waste of time due to the opcode cache not being retained between individual calls. But APC also contains apc_bin_dumpfile() and apc_load_dumpfile() functions.

I was thinking these two function might be used to make APC efficient in CLI mode by having it all compiled sometime outside the batchjob, stored in a single dumpfile and having the individual processes load the dumpfile.

Does anybody have any experience with such a scenario or can you give good reasons why it will or will not work? If any significant gains could reasonably be had, either in memory use or performance? What pitfalls are lurking in the shadows?

Edit
Report

2 Answers

13

Disclaimer: As awesome as APC is when it works in CLI, and it is awesome, it can equally be as frustrating. Use with a healthy load of patience, be thorough, step away from the problem if you're spinning, keep in mind you are working with cache that is why it seems like its doing nothing, it is actually doing nothing. Delete dump file, start with just the basics, if that doesn't work forget it try a new machine, new OS, if it is working make a copy, piece by piece expand functionality - there are loads of things that won't work, if it is working commit or make a copy, add another piece and test again, for sanity-check recheck the copies that were working before, cliches or not; if at first you don't succeed try try again, you can't keep doing the same thing expecting new results.

Ready? This is what you've been waiting for:

Enable apc for cli

apc.enable-cli=1

it is not ideal to create, populate and destroy the APC cache on every CLI request

   - previous answer by unknown poster since removed.

You're absolutely right that sucks, lets fix it shall we?

If you try and use APC under CLI and it is not enabled you will get warnings.

something like:

PHP Warning:  apc_bin_loadfile(): APC is not enabled, 
              apc_bin_loadfile not available.
PHP Warning:  apc_bin_dumpfile(): APC is not enabled, 
              apc_bin_dumpfile not available.

Warning: I suggest you don't enable cli in php.ini, it is not worth the frustration, you are going to forget you did it and have numerous other headaches with other scripts, trust me its not worth it, use a launcher script instead. (see below)

apc_loadfile and apc_dumpfile in cli

As per the comment by answered 2012-08-10T20:51:51.857

0

I would definitely not use it in the CLI as when you restart it, it's almost as if it was never running in the first place!

The better way of using APC is to have it running on the webserver itself all the time, this way with it being active it will actually do what it's supposed to do!

answered 2012-07-24T10:16:40.183

Your Answer