Redis.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Module\DelayQueue;
  3. use App\Module\DelayQueue\Entity\Queue;
  4. use App\Module\DelayQueue\Job\Job;
  5. class Redis
  6. {
  7. const E_KEY = 'delay_queue';
  8. /**
  9. * 添加队列
  10. *
  11. * @param array $callback
  12. * @param $runParam
  13. * @param int $delay
  14. * @return int
  15. */
  16. static public function addQueue($callback, $runParam, $delay = 3): int
  17. {
  18. if (!is_callable($callback)) {
  19. throw new \Exception('callback is not callable');
  20. }
  21. $key = self::E_KEY . $callback[0] . $callback[1].md5(serialize($runParam));
  22. /**
  23. * @var \Redis $a
  24. */
  25. $a = \Illuminate\Support\Facades\Redis::client();
  26. if ($a->exists($key)) {
  27. return 0;
  28. }
  29. $a->setex($key, $delay, 1);
  30. $q = new Queue();
  31. $q->create_ts = time();
  32. $q->delay_ts = $delay;
  33. $q->runClass = $callback[0];
  34. $q->runMethod = $callback[1];
  35. $q->runParam = $runParam;
  36. Job::dispatch($q)->delay($delay);
  37. return 1;
  38. }
  39. }