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?
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
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:
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 mightye php we need to disable apc.stat or you will get a warnings
something like:
launcher script – php-apc.sh
We will use this script to launch our apc enabled scripts (ex.
./php-apc.sh apc-cli.php) instead of changing the properties inphp.inidirectly.Ready for the basic functionality? Sure you are =)
basic APC persisted – apc-cli.php
Notice: Why not use file_exists? Because
file_exists == statyou see and we want to reap the reward that isapc.stat=0so; work within the include path; use absolute and not relative paths – as returned bystream_resolve_include_path();avoidinclude_once,require_onceuse the non*_oncecounterparts; check your stat usage, when not using APC(Muchos important senor), with the help of a StreamWrapper echo for calls to methodurl_stat;Oops: Fatal scope over-run error! aborting notice thread. see url_statmessage: Error caused by StreamWrapper outside the scope of this discussion.
The smoke test
Using the launcher execute the basic script
A whole bunch of nothing happened that’s what we want right, why else do you want to use cache? If it did output anything then it didn’t work, sorry.
There should be a dump file called apc.dump see if you can find it? If you can’t find it then it didn’t work, sorry.
Good we have the dump file there were no errors lets run it again.
What you want to see:
Success! =)
There are few in PHP as satisfying as a working APC implementation.
nJoy!