Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache/data'),
'lock_path' => storage_path('framework/cache/data'),
'lock_path' => storage_path('framework/cache/locks'),
],

'memcached' => [
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,12 @@ public function forever($key, $value)
*/
public function lock($name, $seconds = 0, $owner = null)
{
$this->ensureCacheDirectoryExists($this->lockDirectory ?? $this->directory);
$locksDir = $this->lockDirectory ?? ($this->directory.'/locks');

$this->ensureCacheDirectoryExists($locksDir);

return new FileLock(
new static($this->files, $this->lockDirectory ?? $this->directory, $this->filePermission),
new static($this->files, $locksDir, $this->filePermission),
$name,
$seconds,
$owner
Expand Down
26 changes: 26 additions & 0 deletions tests/Integration/Cache/FileCacheLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Sleep;
use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\TestCase;
use Throwable;

#[WithConfig('cache.default', 'file')]
class FileCacheLockTest extends TestCase
Expand All @@ -16,6 +17,9 @@ protected function setUp(): void
{
parent::setUp();

$this->app['config']->set('cache.default', 'file');
$this->app['config']->set('cache.stores.file.lock_path', storage_path('framework/cache/locks'));

// flush lock from previous tests
Cache::lock('foo')->forceRelease();
}
Expand Down Expand Up @@ -100,6 +104,18 @@ public function testOwnerStatusCanBeCheckedAfterRestoringLock()
$this->assertTrue($secondLock->isOwnedByCurrentProcess());
}

public function testCacheRememberReturnsValueWhenLockWithSameKeyExists()
{
$lock = Cache::lock('my-key', 5);
$this->assertTrue($lock->get());

$value = Cache::remember('my-key', 60, fn () => 'expected-value');

$this->assertSame('expected-value', $value);

$lock->release();
}

public function testOtherOwnerDoesNotOwnLockAfterRestore()
{
$firstLock = Cache::lock('foo', 10);
Expand All @@ -123,4 +139,14 @@ public function testExceptionIfBlockCanNotAcquireLock()
$this->expectException(LockTimeoutException::class);
Cache::lock('foo', 10)->block(5);
}

protected function tearDown(): void
{
try {
Cache::lock('foo')->forceRelease();
} catch (Throwable) {
}

parent::tearDown();
}
}