PHP code example of woojean / php-redis-queue
1. Go to this page and download the library: Download woojean/php-redis-queue library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
woojean / php-redis-queue example snippets
// queue config
$queueName = 'myqueue';
$redisConfig = [
'host' => '127.0.0.1',
'port' => '6379',
'index' => '0'
];
// add data
$data = [
'k1' => 'v1',
'k2' => 'v2',
];
echo "Queue name: " . $queueName . "\n";
try {
// create queue
$redisQueue = new RedisQueue($queueName, $redisConfig);
while(True){
// add to queue
$index = $redisQueue->add($data);
echo "Data index: " . $index . "\n"; // index of added data
sleep(1);
}
} catch (RedisQueueException $e) {
echo $e->getMessage();
}
// queue config
$queueName = 'myqueue';
$redisConfig = [
'host' => '127.0.0.1',
'port' => '6379',
'index' => '0'
];
echo "Queue name: " . $queueName . "\n";
while (True){
try {
// create queue
$redisQueue = new RedisQueue($queueName, $redisConfig);
// fetch from queue
$data = $redisQueue->get();
echo "Fetched data:\n";
//var_dump($data);
// get the index of current data
echo "Current Index:";
$currentIndex = $redisQueue->getCurrentIndex($data);
var_dump($currentIndex);
echo $currentIndex;
/* ... */
$success = True; // process result
//$success = False; // process result
/* ... */
sleep(2);
if ($success) { // success
$ret = $redisQueue->remove($data);
if (!empty($ret)) {
echo "\nData removed !";
}
} else { // failed
echo "\nRollback current data";
$redisQueue->rollback($data); // if retry times up to max, the index will be transfer to blocked list
}
} catch (RedisQueueException $e) {
echo $e->getMessage();
}
}
$redisQueue = new RedisQueue($queueName, $redisConfig);
$num = $redisQueue->repair();
// queue config
$queueName = 'myqueue';
$redisConfig = [
'host' => '127.0.0.1',
'port' => '6379',
'index' => '2'
];
$redisQueue = new RedisQueue($queueName, $redisConfig);
$ret = $redisQueue->status();
var_dump($ret);