Reputation: 28830
Is it possible to have the PHP server (via php5-fpm) run a PHP script just after it started, and before clients have access to it, in order to perform the initialization of APC variables.
Basically some events are counted during the server life via apc_inc
, like
apc_inc('event-xyz-happened');
the event-xyz-happened APC var is permanent (life span is server life, not request life).
The problem is, the event-xyz-happened APC var has to exist before it is incremented (unlike Perl) the first time. apc_inc
being pretty fast, I want to avoid solutions like
if ( ! apc_exists('event-xyz-happened')) {
apc_store('event-xyz-happened', 1);
}
else {
apc_inc('event-xyz-happened');
}
which not only require a call to apc_exists('event-xyz-happened')
, but may also suffer from a race condition when it doesn't exist yet.
--
Is there a solution to create some APC variables before clients have access to the server?
Upvotes: 0
Views: 489
Reputation: 2418
You can use apc_add followed by apc_inc (see http://www.php.net/manual/en/function.apc-add.php)
// if it doesn't exist, it gets created
// if it does exist, nothing happens, no race condition
apc_add('event-xyz-happened', 0);
apc_inc('event-xyz-happened', 1);
Upvotes: 2
Reputation: 3446
You should not use apc variables for this purpose.
APC is a cache engine, it is not a fast database engine. As a cache engine, it can and it will sooner or later remove your variables to clear some memory for other variables or opcode cache. The more memory you assinge to APC the less probable it is that your variable will get removed, but you cannot rely that the variable will be there.
All your php scripts have to check if the variable is in apc cache, and if it is not, initialize it.
If you need to store some variables with very quick access, you can setup a local mysql server and create a table with 'memory' engine. It will be almost as fast as apc, but i will be permament, as long as the server is running.
Good luck SWilk
Upvotes: 0