-
Notifications
You must be signed in to change notification settings - Fork 325
Closed
Labels
Description
Description of issue:
The returned value of the first increment in the following code is incorrect. As far as I can tell, the Memcached server is incrementing the value correctly, but somewhere along the line the value that is actually returned by the command is not the same as the stored value.
Code:
<?php
//Setup MemcacheD
{
$memcached = new Memcached();
$memcached->setOptions(array(memcached::OPT_BINARY_PROTOCOL => true));
$memcached->addServers(array(array('127.0.0.1', 11211, 100)));
}
// Create a key for use as a lock. If this key already exists, wait till it doesn't exist.
{
$key = 'LockKey';
$lockToken = mt_rand(0, pow(2, 32)); //Random value betwen 0 and 2^32 for ownership verification
while (true)
{
$casToken = null;
$data = $memcached->get($key, $casToken);
if ($memcached->getResultCode() == Memcached::RES_NOTFOUND)
{
if ($memcached->add($key, $lockToken, 5))
{
break;
}
}
elseif ($data === false)
{
if ($memcached->cas($casToken, $key, $lockToken, 5))
{
break;
}
}
//Sleep 10 milliseconds
usleep(10 * 1000);
}
}
//Do something here that requires exclusive access to this key
//Effectively delete our key lock.
{
$casToken = null;
if ($lockToken == $memcached->get($key, $casToken))
{
$memcached->cas($casToken, $key, false, 1);
}
}
//Create 10 keys and then increment them. The first value returned will be wrong.
{
$keyList = array();
for ($i = 0; $i < 10; $i++)
{
$keyList[] = $i . '_count__value';
}
$valueList = array();
foreach ($keyList as $key)
{
$valueList[$key] = $memcached->increment($key, 1, 1);
}
echo '<pre>' . print_r($valueList, true) . '</pre>';
}Actual Output:
Array
(
[0_count__value] => 4294967296
[1_count__value] => 1
[2_count__value] => 1
[3_count__value] => 1
[4_count__value] => 1
[5_count__value] => 1
[6_count__value] => 1
[7_count__value] => 1
[8_count__value] => 1
[9_count__value] => 1
)Expected Output:
Array
(
[0_count__value] => 1
[1_count__value] => 1
[2_count__value] => 1
[3_count__value] => 1
[4_count__value] => 1
[5_count__value] => 1
[6_count__value] => 1
[7_count__value] => 1
[8_count__value] => 1
[9_count__value] => 1
)