PHP Test
Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Redis->lRange()
Testing
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
<?php
function minOrMax(&$which, $a, $b) {
if (rand(1, 2) == 1) {
$which = "MAX";
return max($a, $b);
} else {
$max = "MIN";
return min($a, $b);
}
}
$obj_r = new Redis();
$obj_r->connect('127.0.0.1', 6379);
$obj_r->del('messages');
for ($n = 0; $n < 10; $n++) {
$a = rand(1, 1000);
$b = rand(1, 1000);
$ret = minOrMax($which, $a, $b);
$msg = "$which($a, $b) = $ret";
$obj_r->rpush('messages', $msg);
}
foreach ($obj_r->lRange('messages', 0, -1) as $msg) {
echo "Entry: $msg\n";
}
Enter to Rename, Shift+Enter to Preview
Redis->zRange()
Testing
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
<?php
function minOrMax(&$which, $a, $b) {
if (rand(1, 2) == 1) {
$which = "MAX";
return max($a, $b);
} else {
$max = "MIN";
return min($a, $b);
}
}
$obj_r = new Redis();
$obj_r->connect('127.0.0.1', 6379);
$obj_r->del('zmessages');
for ($n = 0; $n < 10; $n++) {
$a = rand(1, 1000);
$b = rand(1, 1000);
$ret = minOrMax($which, $a, $b);
$msg = "$which($a, $b) = $ret";
$obj_r->zAdd('zmessages', $ret, $msg);
}
foreach ($obj_r->zRange('zmessages', 0, -1, true) as $entry => $score) {
echo "$entry: $score\n";
}
Enter to Rename, Shift+Enter to Preview
Redis->getBit()
Redis->getBit($str_key, $i_position): int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$obj_r = new Redis();
$obj_r->connect('127.0.0.1', 6379);
// 0111 1111
$obj_r->set('key', "\x7f");
/* Should print: 0 */
echo $obj_r->getBit('key', 0) . "\n";
/* Should print: 1 */
echo $obj_r->getBit('key', 1) . "\n";
Enter to Rename, Shift+Enter to Preview
Redis->scan()
Redis->scan(&$it [, $str_pattern, $count]): array
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
<?php
$obj_r = new Redis();
$obj_r->connect('127.0.0.1', 6379);
/* Without enabling Redis::SCAN_RETRY (default condition) */
$it = NULL;
do {
// Scan for some keys
$arr_keys = $obj_r->scan($it);
// Redis may return empty results, so protect against that
if ($arr_keys !== FALSE) {
foreach($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
} while ($it > 0);
echo "No more keys to scan!\n";
/* With Redis::SCAN_RETRY enabled */
$obj_r->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$it = NULL;
/* phpredis will retry the SCAN command if empty results are returned from the
server, so no empty results check is required. */
while ($arr_keys = $obj_r->scan($it)) {
foreach ($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
}
echo "\nNo more keys to scan! \o/\n";
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content