| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace App\Module\DelayQueue;
- use App\Module\DelayQueue\Entity\Queue;
- use App\Module\DelayQueue\Job\Job;
- class Redis
- {
- const E_KEY = 'delay_queue';
- /**
- * 添加队列
- *
- * @param array $callback
- * @param $runParam
- * @param int $delay
- * @return int
- */
- static public function addQueue($callback, $runParam, $delay = 3): int
- {
- if (!is_callable($callback)) {
- throw new \Exception('callback is not callable');
- }
- $key = self::E_KEY . $callback[0] . $callback[1].md5(serialize($runParam));
- /**
- * @var \Redis $a
- */
- $a = \Illuminate\Support\Facades\Redis::client();
- if ($a->exists($key)) {
- return 0;
- }
- $a->setex($key, $delay, 1);
- $q = new Queue();
- $q->create_ts = time();
- $q->delay_ts = $delay;
- $q->runClass = $callback[0];
- $q->runMethod = $callback[1];
- $q->runParam = $runParam;
- Job::dispatch($q)->delay($delay);
- return 1;
- }
- }
|