0

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?

2
  • dunno about php5-fpm but I know that at work we have the following in our virtual host config in apache which looks to achieve a similar thing to what you're after: php_value auto_prepend_file /var/www/xhprof_gui/xhprof_session.php Commented Feb 13, 2013 at 9:23
  • @Jason this is to set configuration directives, not APC variables (plus, I'm using nginx) Commented Feb 13, 2013 at 9:25

2 Answers 2

2

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

Good idea indeed. I'd still prefer an initialization solution, that would avoid the apc_add overhead. But that's the best alternative so far.
0

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

3 Comments

This is not true. If no ttl is specified, the variable will not expire (as long as there is memory available). As for creating a memory table in a DB, I'm after performance, and I doubt an inc+query+fetch... from a database can be even close to apc_inc (which returns the incremented value) in terms of performance.
I am just saying that you cannot rely on the variable beeing in cache for sure. If you run out of memory and no variable ttl has run out then entire cache will be purged. See stackoverflow.com/questions/1053810/… As of mysql memory table performance, I might have exaggerated a bit ;)
Sure, thanks, and I rely on the cache. This is why all precaution is taken - automatic checking scripts, plenty of mem - not to overflow the memory allocated to the cache. Memory will not be overflowed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.