Skip to content

Commit 2693b6d

Browse files
Merge pull request #14 from laravel/master
Master
2 parents 3c62c39 + 2b0f2e9 commit 2693b6d

File tree

7 files changed

+304
-204
lines changed

7 files changed

+304
-204
lines changed

src/Illuminate/Cache/ArrayStore.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Illuminate\Cache;
44

5+
use Illuminate\Support\InteractsWithTime;
6+
57
class ArrayStore extends TaggableStore
68
{
7-
use RetrievesMultipleKeys;
9+
use InteractsWithTime, RetrievesMultipleKeys;
810

911
/**
1012
* The array of stored values.
@@ -21,7 +23,21 @@ class ArrayStore extends TaggableStore
2123
*/
2224
public function get($key)
2325
{
24-
return $this->storage[$key] ?? null;
26+
if (! isset($this->storage[$key])) {
27+
return null;
28+
}
29+
30+
$item = $this->storage[$key];
31+
32+
$expiresAt = $item['expiresAt'] ?? 0;
33+
34+
if ($expiresAt !== 0 && $this->currentTime() > $expiresAt) {
35+
$this->forget($key);
36+
37+
return null;
38+
}
39+
40+
return $item['value'];
2541
}
2642

2743
/**
@@ -34,7 +50,10 @@ public function get($key)
3450
*/
3551
public function put($key, $value, $minutes)
3652
{
37-
$this->storage[$key] = $value;
53+
$this->storage[$key] = [
54+
'value' => $value,
55+
'expiresAt' => $this->calculateExpiration($minutes),
56+
];
3857

3958
return true;
4059
}
@@ -48,10 +67,15 @@ public function put($key, $value, $minutes)
4867
*/
4968
public function increment($key, $value = 1)
5069
{
51-
$this->storage[$key] = ! isset($this->storage[$key])
52-
? $value : ((int) $this->storage[$key]) + $value;
70+
if (! isset($this->storage[$key])) {
71+
$this->forever($key, $value);
5372

54-
return $this->storage[$key];
73+
return $this->storage[$key]['value'];
74+
}
75+
76+
$this->storage[$key]['value'] = ((int) $this->storage[$key]['value']) + $value;
77+
78+
return $this->storage[$key]['value'];
5579
}
5680

5781
/**
@@ -112,4 +136,26 @@ public function getPrefix()
112136
{
113137
return '';
114138
}
139+
140+
/**
141+
* Get the expiration time of the key.
142+
*
143+
* @param int $minutes
144+
* @return int
145+
*/
146+
protected function calculateExpiration($minutes)
147+
{
148+
return $this->toTimestamp($minutes);
149+
}
150+
151+
/**
152+
* Get the UNIX timestamp for the given number of minutes.
153+
*
154+
* @param int $minutes
155+
* @return int
156+
*/
157+
protected function toTimestamp($minutes)
158+
{
159+
return $minutes > 0 ? $this->availableAt($minutes * 60) : 0;
160+
}
115161
}

src/Illuminate/Queue/BeanstalkdQueue.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,26 @@ class BeanstalkdQueue extends Queue implements QueueContract
3030
*/
3131
protected $timeToRun;
3232

33+
/**
34+
* The maximum number of seconds to block for a job.
35+
*
36+
* @var int
37+
*/
38+
protected $blockFor;
39+
3340
/**
3441
* Create a new Beanstalkd queue instance.
3542
*
3643
* @param \Pheanstalk\Pheanstalk $pheanstalk
3744
* @param string $default
3845
* @param int $timeToRun
46+
* @param int $blockFor
3947
* @return void
4048
*/
41-
public function __construct(Pheanstalk $pheanstalk, $default, $timeToRun)
49+
public function __construct(Pheanstalk $pheanstalk, $default, $timeToRun, $blockFor = 0)
4250
{
4351
$this->default = $default;
52+
$this->blockFor = $blockFor;
4453
$this->timeToRun = $timeToRun;
4554
$this->pheanstalk = $pheanstalk;
4655
}
@@ -117,7 +126,7 @@ public function pop($queue = null)
117126
{
118127
$queue = $this->getQueue($queue);
119128

120-
$job = $this->pheanstalk->watchOnly($queue)->reserve(0);
129+
$job = $this->pheanstalk->watchOnly($queue)->reserve($this->blockFor);
121130

122131
if ($job instanceof PheanstalkJob) {
123132
return new BeanstalkdJob(

src/Illuminate/Queue/Connectors/BeanstalkdConnector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ class BeanstalkdConnector implements ConnectorInterface
1717
*/
1818
public function connect(array $config)
1919
{
20-
$retryAfter = $config['retry_after'] ?? Pheanstalk::DEFAULT_TTR;
21-
22-
return new BeanstalkdQueue($this->pheanstalk($config), $config['queue'], $retryAfter);
20+
return new BeanstalkdQueue(
21+
$this->pheanstalk($config),
22+
$config['queue'],
23+
$config['retry_after'] ?? Pheanstalk::DEFAULT_TTR,
24+
$config['block_for'] ?? 0
25+
);
2326
}
2427

2528
/**

tests/Cache/CacheArrayStoreTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Cache;
44

5+
use Illuminate\Support\Carbon;
56
use PHPUnit\Framework\TestCase;
67
use Illuminate\Cache\ArrayStore;
78

@@ -33,6 +34,19 @@ public function testMultipleItemsCanBeSetAndRetrieved()
3334
], $store->many(['foo', 'fizz', 'quz', 'norf']));
3435
}
3536

37+
public function testItemsCanExpire(): void
38+
{
39+
Carbon::setTestNow(Carbon::now());
40+
$store = new ArrayStore;
41+
42+
$store->put('foo', 'bar', 10);
43+
Carbon::setTestNow(Carbon::now()->addMinutes(10)->addSecond());
44+
$result = $store->get('foo');
45+
46+
$this->assertNull($result);
47+
Carbon::setTestNow(null);
48+
}
49+
3650
public function testStoreItemForeverProperlyStoresInArray()
3751
{
3852
$mock = $this->getMockBuilder(ArrayStore::class)->setMethods(['put'])->getMock();
@@ -47,22 +61,25 @@ public function testValuesCanBeIncremented()
4761
{
4862
$store = new ArrayStore;
4963
$store->put('foo', 1, 10);
50-
$store->increment('foo');
64+
$result = $store->increment('foo');
65+
$this->assertEquals(2, $result);
5166
$this->assertEquals(2, $store->get('foo'));
5267
}
5368

5469
public function testNonExistingKeysCanBeIncremented()
5570
{
5671
$store = new ArrayStore;
57-
$store->increment('foo');
72+
$result = $store->increment('foo');
73+
$this->assertEquals(1, $result);
5874
$this->assertEquals(1, $store->get('foo'));
5975
}
6076

6177
public function testValuesCanBeDecremented()
6278
{
6379
$store = new ArrayStore;
6480
$store->put('foo', 1, 10);
65-
$store->decrement('foo');
81+
$result = $store->decrement('foo');
82+
$this->assertEquals(0, $result);
6683
$this->assertEquals(0, $store->get('foo'));
6784
}
6885

0 commit comments

Comments
 (0)