Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP : apc_store doesn't work as intended

Tags:

php

caching

apc

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)

like image 783
Industrial Avatar asked May 26 '10 18:05

Industrial


1 Answers

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).

like image 72
Bill Karwin Avatar answered Nov 12 '22 20:11

Bill Karwin