3 include (__DIR__
. '/redis.inc');
5 $r = NewRedisTestInstance();
6 ExtRedisScan
::$prefix = GetTestKeyName(__FILE__
) . ':';
7 $r->setOption(Redis
::OPT_PREFIX
, ExtRedisScan
::$prefix);
8 $r->setOption(Redis
::OPT_SCAN
, Redis
::SCAN_RETRY
);
9 $ret = $r->delete('scan');
10 $ret = $r->mset(array('key:one' => 'one', 'key:two' => 'two',
11 'key:three' => 'three','key:four' => 'four'));
14 * The PHP5 extension we're patterning after doesn't try to turn
15 * OPT_PREFIX into a pattern when doing scan(), so scan returns
16 * global state. Filter it ourselves.
18 function performScan($fn){
21 while (($retval = $fn($cursor))){
22 foreach ($retval as $key){
24 if (strstr($key, ExtRedisScan
::$prefix) !== false){
25 $returns []= substr($key, strlen(ExtRedisScan
::$prefix));
29 // Normalize return order
35 $ret = performScan(function(&$cursor) use($r){
36 // Scan without patterns.
37 return $r->scan($cursor);
41 $ret = performScan(function(&$cursor) use($r){
42 // catch key:two and key:three
44 return $r->scan($cursor, ExtRedisScan
::$prefix . 'key:t*');
48 $ret = performScan(function(&$cursor) use($r){
51 return $r->scan($cursor, ExtRedisScan
::$prefix . 'nokey:t*');
55 $r->delete('key:one');
56 $r->delete('key:two');
57 $r->delete('key:three');
58 $r->delete('key:four');
61 abstract final class ExtRedisScan
{
62 public static $prefix;