Skip to content

Commit 59aecc5

Browse files
faisuctimacdonaldtaylorotwell
authored
[12.x] Namespace file cache lock keys (#57516)
* Use separate lock directory to prevent key collisions in file cache * Update FileCacheLockTest.php * [12.x] Namespace file cache lock keys * Update FileStore.php * Update src/Illuminate/Cache/FileStore.php Co-authored-by: Tim MacDonald <[email protected]> * formatting --------- Co-authored-by: Tim MacDonald <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5a81abb commit 59aecc5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Illuminate/Cache/FileStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function lock($name, $seconds = 0, $owner = null)
220220

221221
return new FileLock(
222222
new static($this->files, $this->lockDirectory ?? $this->directory, $this->filePermission),
223-
$name,
223+
"file-store-lock:{$name}",
224224
$seconds,
225225
$owner
226226
);

tests/Integration/Cache/FileCacheLockTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Sleep;
99
use Orchestra\Testbench\Attributes\WithConfig;
1010
use Orchestra\Testbench\TestCase;
11+
use Throwable;
1112

1213
#[WithConfig('cache.default', 'file')]
1314
class FileCacheLockTest extends TestCase
@@ -100,6 +101,18 @@ public function testOwnerStatusCanBeCheckedAfterRestoringLock()
100101
$this->assertTrue($secondLock->isOwnedByCurrentProcess());
101102
}
102103

104+
public function testCacheRememberReturnsValueWhenLockWithSameKeyExists()
105+
{
106+
$lock = Cache::lock('my-key', 5);
107+
$this->assertTrue($lock->get());
108+
109+
$value = Cache::remember('my-key', 60, fn () => 'expected-value');
110+
111+
$this->assertSame('expected-value', $value);
112+
113+
$lock->release();
114+
}
115+
103116
public function testOtherOwnerDoesNotOwnLockAfterRestore()
104117
{
105118
$firstLock = Cache::lock('foo', 10);
@@ -123,4 +136,14 @@ public function testExceptionIfBlockCanNotAcquireLock()
123136
$this->expectException(LockTimeoutException::class);
124137
Cache::lock('foo', 10)->block(5);
125138
}
139+
140+
protected function tearDown(): void
141+
{
142+
try {
143+
Cache::lock('foo')->forceRelease();
144+
} catch (Throwable) {
145+
}
146+
147+
parent::tearDown();
148+
}
126149
}

0 commit comments

Comments
 (0)