下面是作者给出的两个简单实例:
实例一:
$libredis = Libredis();
$connection = $libredis->get_connection("127.0.0.1");
//set a key
$batch = $libredis->create_batch();
$batch->set('hello', 'world');
$batch->execute($connection);
//now fetch the key
$batch = $libredis->create_batch();
$batch->get('hello');
$batch->execute($connection);
while($batch->next_reply($reply_type, $reply_value, $reply_length)) {
echo $reply_value, PHP_EOL;
}
实例二:(multiple server模式)
$libredis = Libredis();
$connection1 = $libredis->get_connection("127.0.0.1:6379");
$connection2 = $libredis->get_connection("127.0.0.1:6380");
$batch1 = $libredis->create_batch();
$batch1->set('hello', 'world'); //add a 'set' command to batch1
$batch2 = $libredis->create_batch();
$batch2->set('hello2', 'world2');
$executor = $libredis->create_executor();
$executor->add($connection1, $batch1); //associate connection1 with batch1
$executor->add($connection2, $batch2); //associate connection2 with batch2
//execute all batches against their corresponding connections in parallel, with a
//500 ms timeout on the whole operation.
$executor->execute(500);