CacheSchedulingMutex.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Illuminate\Console\Scheduling;
  3. use DateTimeInterface;
  4. use Illuminate\Contracts\Cache\Factory as Cache;
  5. class CacheSchedulingMutex implements SchedulingMutex
  6. {
  7. /**
  8. * The cache factory implementation.
  9. *
  10. * @var \Illuminate\Contracts\Cache\Factory
  11. */
  12. public $cache;
  13. /**
  14. * The cache store that should be used.
  15. *
  16. * @var string|null
  17. */
  18. public $store;
  19. /**
  20. * Create a new scheduling strategy.
  21. *
  22. * @param \Illuminate\Contracts\Cache\Factory $cache
  23. * @return void
  24. */
  25. public function __construct(Cache $cache)
  26. {
  27. $this->cache = $cache;
  28. }
  29. /**
  30. * Attempt to obtain a scheduling mutex for the given event.
  31. *
  32. * @param \Illuminate\Console\Scheduling\Event $event
  33. * @param \DateTimeInterface $time
  34. * @return bool
  35. */
  36. public function create(Event $event, DateTimeInterface $time)
  37. {
  38. return $this->cache->store($this->store)->add(
  39. $event->mutexName().$time->format('Hi'), true, 60
  40. );
  41. }
  42. /**
  43. * Determine if a scheduling mutex exists for the given event.
  44. *
  45. * @param \Illuminate\Console\Scheduling\Event $event
  46. * @param \DateTimeInterface $time
  47. * @return bool
  48. */
  49. public function exists(Event $event, DateTimeInterface $time)
  50. {
  51. return $this->cache->store($this->store)->has(
  52. $event->mutexName().$time->format('Hi')
  53. );
  54. }
  55. /**
  56. * Specify the cache store that should be used.
  57. *
  58. * @param string $store
  59. * @return $this
  60. */
  61. public function useStore($store)
  62. {
  63. $this->store = $store;
  64. return $this;
  65. }
  66. }