1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
--TEST--
Test for GH #90
--SKIPIF--
<?php include "skipif.inc";?>
--FILE--
<?php
include dirname (__FILE__) . '/config.inc';
$memcached = memc_get_instance (array (
Memcached::OPT_BINARY_PROTOCOL => true
));
// 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, mt_getrandmax()); //Random value 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 . '_' . uniqid ('count_value_');
}
$valueList = array();
foreach ($keyList as $key)
{
$valueList[$key] = $memcached->increment($key, 1, 1);
}
var_dump ($valueList);
}
--EXPECTF--
array(10) {
["0_%s"]=>
int(1)
["1_%s"]=>
int(1)
["2_%s"]=>
int(1)
["3_%s"]=>
int(1)
["4_%s"]=>
int(1)
["5_%s"]=>
int(1)
["6_%s"]=>
int(1)
["7_%s"]=>
int(1)
["8_%s"]=>
int(1)
["9_%s"]=>
int(1)
}
|