Back
Close

PHP Test

michael-grunder
3,237 views

Redis->lRange()

Testing
<?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";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Redis->zRange()

Testing
<?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";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Redis->getBit()

Redis->getBit($str_key, $i_position): int
<?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";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Redis->scan()

Redis->scan(&$it [, $str_pattern, $count]): array
<?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";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants