CacheEventMutex.php 1.7KB

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