Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 2f1a067

Browse files
author
Gab
committed
Create separate methods popOne() and popMany() for the php client
1 parent 0515064 commit 2f1a067

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
redis-priority-queue is a simple work queue similar to [Redis lists](https://redis.io/commands#list) with the following added features:
44

55
- An item can be added with a priority (between -9007199254740992 and 9007199254740992)
6+
- Queues are automatically de-duplicated (duplicate items are voided when pushing them)
67
- Multiple items can be popped from the queue at the same time
78
- A queue monitoring tool to easily see how many items are in each queue
89

@@ -20,7 +21,7 @@ redis-cli --eval src/redis-priority-queue.lua null null , [push|pop|peek|count]
2021
redis-cli --eval src/redis-priority-queue.lua null null , push my_super_list my_item
2122
2223
-- Pop an item
23-
redis-cli --eval src/redis-priority-queue.lua null null , pop my_super_list my_item
24+
redis-cli --eval src/redis-priority-queue.lua null null , pop my_super_list
2425
```
2526

2627
## `push`: Push an item in a queue

clients/php/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ PHP client for redis-priority-queue.
1414
use RedisPriorityQueue as RQP;
1515

1616
// Requires
17-
require_once(__DIR__.'/../lib/Lua.php');
18-
require_once(__DIR__.'/../lib/Queue.php');
19-
require_once(__DIR__.'/../lib/Redis.php');
17+
require_once('lib/Lua.php');
18+
require_once('lib/Queue.php');
19+
require_once('lib/Redis.php');
2020

2121
// Create a Redis instance
2222
$r = new RQP\Redis($host, $port, $auth, $dbnum);
@@ -29,10 +29,12 @@ $q->setQueueName('sample_queue');
2929

3030
// Push an item in the queue
3131
$res = $q->push('item');
32-
echo $res;
32+
echo "* Push an item:\n";
33+
echo $res."\n";
3334

3435
// Pop an item from the queue
35-
$res = $q->pop();
36+
$res = $q->popOne();
37+
echo "* Pop an item:\n";
3638
echo json_encode($res)."\n";
3739
```
3840

clients/php/lib/Queue.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,48 @@ public function push(string $item, int $priority = null): int
7676
return $this->run();
7777
}
7878

79+
/**
80+
* popOne function.
81+
*
82+
* @access public
83+
* @param string $orderBy (default: 'asc')
84+
* @return void
85+
*/
86+
public function popOne(string $orderBy = 'asc')
87+
{
88+
// Get item
89+
$item = $this->popMany($orderBy, 1);
90+
91+
// Get first (and unique) item from array
92+
if(is_array($item) && count($item) > 0) {
93+
return $item[0];
94+
}
95+
96+
return null;
97+
}
98+
7999
/**
80100
* pop function.
101+
* Alias to popOne();
81102
*
82103
* @access public
83104
* @param string $orderBy (default: 'asc')
84-
* @param int $numberOfItems (default: 1)
85105
* @return misc
86106
*/
87-
public function pop(string $orderBy = 'asc', int $numberOfItems = 1)
107+
public function pop(string $orderBy = 'asc')
108+
{
109+
return $this->popOne($orderBy);
110+
}
111+
112+
/**
113+
* popMany function.
114+
*
115+
* @access public
116+
* @param string $orderBy (default: 'asc')
117+
* @param int $numberOfItems (default: 1)
118+
* @return void
119+
*/
120+
public function popMany(string $orderBy = 'asc', int $numberOfItems = 1)
88121
{
89122
// Set args
90123
$this->prepareArgs('pop', [$orderBy, $numberOfItems]);

clients/php/tests/pop.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
// Run
1717
echo "* pop (one):\n";
18-
$res = $q->pop();
19-
echo json_encode($res)."\n";
18+
$res = $q->popOne();
19+
echo $res."\n";
2020

2121
// Run
2222
echo "* pop (one, descending):\n";
23-
$res = $q->pop('desc');
24-
echo json_encode($res)."\n";
23+
$res = $q->popOne('desc');
24+
echo $res."\n";
2525

2626
// Run
2727
echo "* pop (many):\n";
28-
$res = $q->pop('asc', MULTIPLE_ITEMS_COUNT);
28+
$res = $q->popMany('asc', MULTIPLE_ITEMS_COUNT);
2929
echo json_encode($res)."\n";

0 commit comments

Comments
 (0)