8

I have started to try APC to store some specific data on each webserver as an complement to memcached.

However, the following code piece is giving me headaches:

echo apc_store('key', 'value');
echo apc_store('key', 'newvalue');
echo apc_fetch('key'); 

// Echoes: value

Memcached example:

$memcached = new Memcached;

$memcached->addServer('localhost', '11211');

$memcached->set('key', 'value1');
echo $memcached->get('key') . '<br />'; // Echoes value1

$memcached->set('key', 'value2');
echo $memcached->get('key'). '<br />'; // Echoes value2

$memcached->set('key', 'value3');
echo $memcached->get('key'). '<br />'; // Echoes value3

Why is apc_store not working as properly?

EDIT: To make sure that no one else is spending two hours on looking for a solution, when this is caused by a bug, here's one: http://pecl.php.net/bugs/bug.php?id=16894&edit=1 (not the most effective, though)

Industrial
  • 41,400
  • 69
  • 194
  • 289

1 Answers1

8

This seems to be a known issue: PECL Bug #16814 New warning "Potential cache slam averted for key"

It appears to allow only one apc_store() per request. I tried this test:

<?php

echo "<p>apc_fetch(key): " . apc_fetch('key') . "</p>\n";
// echo "<p>apc_store(value): " . apc_store('key', 'value') . "</p>\n";
echo "<p>apc_store(newvalue): " . apc_store('key', 'newvalue') . "</p>\n";
echo "<p>apc_fetch(key): " . apc_fetch('key') . "</p>\n";

Play with this, un-comment the second line and see that it does overwrite a key set on a previous request, but you can only store a given key once per request.

The bug log mentions an ini file setting apc.slam_defense which when set to Off may disable this single-write behavior. But I tried it briefly and I couldn't confirm this works. Perhaps you'll have more luck (remember to restart Apache when you change php.ini).

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Could imagine that it was something strange like this. Thanks a lot for your help, Bill! – Industrial May 26 '10 at 20:05
  • By the way, have been a bit slack on the mySQL trees that i asked you about some time ago here on SO. You were saying that you were about to make some tree examples available on Github. Anything new there? – Industrial May 26 '10 at 20:06
  • Updated my post with a link to a solution that might help you or anyone else that are stuck in the same situation and MUST use APC – Industrial May 26 '10 at 20:41
  • 2
    I extended Zend Framework for the tree code so I put my experimental code into their `ZendX` incubator on their svn repository (http://is.gd/cqqWt). Slides are at http://www.slideshare.net/billkarwin/models-for-hierarchical-data – Bill Karwin May 26 '10 at 20:44