1--TEST--
2Test for GH #90
3--SKIPIF--
4<?php include "skipif.inc";?>
5--FILE--
6<?php
7include dirname (__FILE__) . '/config.inc';
8$memcached = memc_get_instance (array (
9									Memcached::OPT_BINARY_PROTOCOL => true
10								));
11
12// Create a key for use as a lock.  If this key already exists, wait till it doesn't exist.
13{
14    $key = 'LockKey';
15    $lockToken = mt_rand(0, mt_getrandmax()); //Random value for ownership verification
16
17    while (true)
18    {
19        $casToken = null;
20        $data = $memcached->get($key, $casToken);
21        if ($memcached->getResultCode() == Memcached::RES_NOTFOUND)
22        {
23            if ($memcached->add($key, $lockToken, 5))
24            {
25                break;
26            }
27        }
28        elseif ($data === false)
29        {
30            if ($memcached->cas($casToken, $key, $lockToken, 5))
31            {
32                break;
33            }
34        }
35
36        //Sleep 10 milliseconds
37        usleep(10 * 1000);
38    }
39}
40
41//Do something here that requires exclusive access to this key
42
43//Effectively delete our key lock.
44{
45    $casToken = null;
46    if ($lockToken == $memcached->get($key, $casToken))
47    {
48        $memcached->cas($casToken, $key, false, 1);
49    }
50}
51
52//Create 10 keys and then increment them.  The first value returned will be wrong.
53{
54    $keyList = array();
55    for ($i = 0; $i < 10; $i++)
56    {
57        $keyList[] = $i . '_' . uniqid ('count_value_');
58    }
59
60    $valueList = array();
61    foreach ($keyList as $key)
62    {
63        $valueList[$key] = $memcached->increment($key, 1, 1);
64    }
65
66    var_dump ($valueList);
67}
68
69--EXPECTF--
70array(10) {
71  ["0_%s"]=>
72  int(1)
73  ["1_%s"]=>
74  int(1)
75  ["2_%s"]=>
76  int(1)
77  ["3_%s"]=>
78  int(1)
79  ["4_%s"]=>
80  int(1)
81  ["5_%s"]=>
82  int(1)
83  ["6_%s"]=>
84  int(1)
85  ["7_%s"]=>
86  int(1)
87  ["8_%s"]=>
88  int(1)
89  ["9_%s"]=>
90  int(1)
91}
92