forked from wayhood/yii2-queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisQueue.php
More file actions
78 lines (67 loc) · 1.82 KB
/
RedisQueue.php
File metadata and controls
78 lines (67 loc) · 1.82 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* Created by PhpStorm.
* User: yiistudio
* Date: 11/29/14
* Time: 5:10 PM
*/
namespace wh\queue;
use yii\redis\Connection;
use Yii;
class RedisQueue extends Queue
{
public $redis = 'redis';
//默认队列名
public $default = 'default';
public function init()
{
parent::init();
if (is_string($this->redis)) {
$this->redis = Yii::$app->get($this->redis);
} elseif (is_array($this->redis)) {
if (!isset($this->redis['class'])) {
$this->redis['class'] = Connection::className();
}
$this->redis = Yii::createObject($this->redis);
}
if (!$this->redis instanceof Connection) {
throw new InvalidConfigException("Queue::redis must be either a Redis connection instance or the application component ID of a Redis connection.");
}
}
/**
* 写入数据到队列
*
* @param $payload
* @param null $queue
* @param array $options
* @return mixed
*/
protected function pushInternal($payload, $queue = null, array $options = [])
{
$this->redis->rpush($this->getQueue($queue), $payload);
$payload = json_decode($payload, true);
return $payload['id'];
}
/**
* 获得队列名
* @param $queue
* @return string
*/
protected function getQueueInternal($queue = null)
{
return ($queue ?: $this->default);
}
/**
* 出队列
* @param null $queue
* @return RedisJob
*/
public function popInternal($queue = null)
{
$payload = $this->redis->lpop($this->getQueue($queue));
if (!is_null($payload)) {
//$this->redis->zadd($queue.':reserved', $this->getTime() + 60, $job);
return new Job($this, $payload, $queue);
}
}
}