Skip to content

Commit 20e2919

Browse files
committed
allow customization of schedule mutex cache store
1 parent 74c54c0 commit 20e2919

File tree

6 files changed

+122
-16
lines changed

6 files changed

+122
-16
lines changed

src/Illuminate/Console/Scheduling/CacheEventMutex.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
namespace Illuminate\Console\Scheduling;
44

5-
use Illuminate\Contracts\Cache\Repository as Cache;
5+
use Illuminate\Contracts\Cache\Factory as Cache;
66

77
class CacheEventMutex implements EventMutex
88
{
99
/**
1010
* The cache repository implementation.
1111
*
12-
* @var \Illuminate\Contracts\Cache\Repository
12+
* @var \Illuminate\Contracts\Cache\Factory
1313
*/
1414
public $cache;
1515

16+
/**
17+
* The cache store that should be used.
18+
*
19+
* @var string|null
20+
*/
21+
public $store;
22+
1623
/**
1724
* Create a new overlapping strategy.
1825
*
19-
* @param \Illuminate\Contracts\Cache\Repository $cache
26+
* @param \Illuminate\Contracts\Cache\Factory $cache
2027
* @return void
2128
*/
2229
public function __construct(Cache $cache)
@@ -32,7 +39,7 @@ public function __construct(Cache $cache)
3239
*/
3340
public function create(Event $event)
3441
{
35-
return $this->cache->add(
42+
return $this->cache->store($this->store)->add(
3643
$event->mutexName(), true, $event->expiresAt
3744
);
3845
}
@@ -45,7 +52,7 @@ public function create(Event $event)
4552
*/
4653
public function exists(Event $event)
4754
{
48-
return $this->cache->has($event->mutexName());
55+
return $this->cache->store($this->store)->has($event->mutexName());
4956
}
5057

5158
/**
@@ -56,6 +63,19 @@ public function exists(Event $event)
5663
*/
5764
public function forget(Event $event)
5865
{
59-
$this->cache->forget($event->mutexName());
66+
$this->cache->store($this->store)->forget($event->mutexName());
67+
}
68+
69+
/**
70+
* Specify the cache store that should be used.
71+
*
72+
* @param string $store
73+
* @return $this
74+
*/
75+
public function useStore($store)
76+
{
77+
$this->store = $store;
78+
79+
return $this;
6080
}
6181
}

src/Illuminate/Console/Scheduling/CacheSchedulingMutex.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
namespace Illuminate\Console\Scheduling;
44

55
use DateTimeInterface;
6-
use Illuminate\Contracts\Cache\Repository as Cache;
6+
use Illuminate\Contracts\Cache\Factory as Cache;
77

88
class CacheSchedulingMutex implements SchedulingMutex
99
{
1010
/**
11-
* The cache repository implementation.
11+
* The cache factory implementation.
1212
*
13-
* @var \Illuminate\Contracts\Cache\Repository
13+
* @var \Illuminate\Contracts\Cache\Factory
1414
*/
1515
public $cache;
1616

17+
/**
18+
* The cache store that should be used.
19+
*
20+
* @var string|null
21+
*/
22+
public $store;
23+
1724
/**
1825
* Create a new overlapping strategy.
1926
*
20-
* @param \Illuminate\Contracts\Cache\Repository $cache
27+
* @param \Illuminate\Contracts\Cache\Factory $cache
2128
* @return void
2229
*/
2330
public function __construct(Cache $cache)
@@ -34,7 +41,9 @@ public function __construct(Cache $cache)
3441
*/
3542
public function create(Event $event, DateTimeInterface $time)
3643
{
37-
return $this->cache->add($event->mutexName().$time->format('Hi'), true, 60);
44+
return $this->cache->store($this->store)->add(
45+
$event->mutexName().$time->format('Hi'), true, 60
46+
);
3847
}
3948

4049
/**
@@ -46,6 +55,21 @@ public function create(Event $event, DateTimeInterface $time)
4655
*/
4756
public function exists(Event $event, DateTimeInterface $time)
4857
{
49-
return $this->cache->has($event->mutexName().$time->format('Hi'));
58+
return $this->cache->store($this->store)->has(
59+
$event->mutexName().$time->format('Hi')
60+
);
61+
}
62+
63+
/**
64+
* Specify the cache store that should be used.
65+
*
66+
* @param string $store
67+
* @return $this
68+
*/
69+
public function useStore($store)
70+
{
71+
$this->store = $store;
72+
73+
return $this;
5074
}
5175
}

src/Illuminate/Console/Scheduling/Schedule.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,23 @@ public function events()
174174
{
175175
return $this->events;
176176
}
177+
178+
/**
179+
* Specify the cache store that should be used to store mutexes.
180+
*
181+
* @param string $store
182+
* @return $this
183+
*/
184+
public function useCache($store)
185+
{
186+
if ($this->eventMutex instanceof CacheEventMutex) {
187+
$this->eventMutex->useStore($store);
188+
}
189+
190+
if ($this->schedulingMutex instanceof CacheSchedulingMutex) {
191+
$this->schedulingMutex->useStore($store);
192+
}
193+
194+
return $this;
195+
}
177196
}

tests/Console/ConsoleEventSchedulerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Mockery as m;
66
use PHPUnit\Framework\TestCase;
7+
use Illuminate\Container\Container;
78
use Illuminate\Console\Scheduling\Schedule;
9+
use Illuminate\Console\Scheduling\EventMutex;
10+
use Illuminate\Console\Scheduling\SchedulingMutex;
811

912
class ConsoleEventSchedulerTest extends TestCase
1013
{
1114
public function setUp()
1215
{
1316
parent::setUp();
1417

15-
$container = \Illuminate\Container\Container::getInstance();
18+
$container = Container::getInstance();
1619

1720
$container->instance('Illuminate\Console\Scheduling\EventMutex', m::mock('Illuminate\Console\Scheduling\CacheEventMutex'));
1821

@@ -28,6 +31,14 @@ public function tearDown()
2831
m::close();
2932
}
3033

34+
public function testMutexCanReceiveCustomStore()
35+
{
36+
Container::getInstance()->make(EventMutex::class)->shouldReceive('useStore')->once()->with('test');
37+
Container::getInstance()->make(SchedulingMutex::class)->shouldReceive('useStore')->once()->with('test');
38+
39+
$this->schedule->useCache('test');
40+
}
41+
3142
public function testExecCreatesNewCommand()
3243
{
3344
$escape = '\\' === DIRECTORY_SEPARATOR ? '"' : '\'';

tests/Console/Scheduling/CacheEventMutexTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class CacheEventMutexTest extends TestCase
1919
*/
2020
protected $event;
2121

22+
/**
23+
* @var \Illuminate\Contracts\Cache\Factory
24+
*/
25+
protected $cacheFactory;
26+
2227
/**
2328
* @var \Illuminate\Contracts\Cache\Repository
2429
*/
@@ -28,8 +33,10 @@ public function setUp()
2833
{
2934
parent::setUp();
3035

36+
$this->cacheFactory = m::mock('Illuminate\Contracts\Cache\Factory');
3137
$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
32-
$this->cacheMutex = new CacheEventMutex($this->cacheRepository);
38+
$this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository);
39+
$this->cacheMutex = new CacheEventMutex($this->cacheFactory);
3340
$this->event = new Event($this->cacheMutex, 'command');
3441
}
3542

@@ -40,6 +47,15 @@ public function testPreventOverlap()
4047
$this->cacheMutex->create($this->event);
4148
}
4249

50+
public function testCustomConnection()
51+
{
52+
$this->cacheFactory->shouldReceive('store')->with('test')->andReturn($this->cacheRepository);
53+
$this->cacheRepository->shouldReceive('add')->once();
54+
$this->cacheMutex->useStore('test');
55+
56+
$this->cacheMutex->create($this->event);
57+
}
58+
4359
public function testPreventOverlapFails()
4460
{
4561
$this->cacheRepository->shouldReceive('add')->once()->andReturn(false);

tests/Console/Scheduling/CacheSchedulingMutexTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class CacheSchedulingMutexTest extends TestCase
2626
*/
2727
protected $time;
2828

29+
/**
30+
* @var \Illuminate\Contracts\Cache\Factory
31+
*/
32+
protected $cacheFactory;
33+
2934
/**
3035
* @var \Illuminate\Contracts\Cache\Repository
3136
*/
@@ -35,9 +40,11 @@ public function setUp()
3540
{
3641
parent::setUp();
3742

43+
$this->cacheFactory = m::mock('Illuminate\Contracts\Cache\Factory');
3844
$this->cacheRepository = m::mock('Illuminate\Contracts\Cache\Repository');
39-
$this->cacheMutex = new CacheSchedulingMutex($this->cacheRepository);
40-
$this->event = new Event(new CacheEventMutex($this->cacheRepository), 'command');
45+
$this->cacheFactory->shouldReceive('store')->andReturn($this->cacheRepository);
46+
$this->cacheMutex = new CacheSchedulingMutex($this->cacheFactory);
47+
$this->event = new Event(new CacheEventMutex($this->cacheFactory), 'command');
4148
$this->time = Carbon::now();
4249
}
4350

@@ -48,6 +55,15 @@ public function testMutexReceviesCorrectCreate()
4855
$this->assertTrue($this->cacheMutex->create($this->event, $this->time));
4956
}
5057

58+
public function testCanUseCustomConnection()
59+
{
60+
$this->cacheFactory->shouldReceive('store')->with('test')->andReturn($this->cacheRepository);
61+
$this->cacheRepository->shouldReceive('add')->once()->with($this->event->mutexName().$this->time->format('Hi'), true, 60)->andReturn(true);
62+
$this->cacheMutex->useStore('test');
63+
64+
$this->assertTrue($this->cacheMutex->create($this->event, $this->time));
65+
}
66+
5167
public function testPreventsMultipleRuns()
5268
{
5369
$this->cacheRepository->shouldReceive('add')->once()->with($this->event->mutexName().$this->time->format('Hi'), true, 60)->andReturn(false);

0 commit comments

Comments
 (0)