Skip to content

Commit fcb6558

Browse files
authored
[9.x] Fix race condition in locks issued by the file cache driver (#46011)
* Fix file lock race condition * wip * revert first commit * wip
1 parent c72459a commit fcb6558

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Illuminate/Cache/FileLock.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Illuminate\Cache;
4+
5+
class FileLock extends CacheLock
6+
{
7+
/**
8+
* Attempt to acquire the lock.
9+
*
10+
* @return bool
11+
*/
12+
public function acquire()
13+
{
14+
return $this->store->add($this->name, $this->owner, $this->seconds);
15+
}
16+
}

src/Illuminate/Cache/FileStore.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class FileStore implements Store, LockProvider
1414
{
15-
use InteractsWithTime, HasCacheLock, RetrievesMultipleKeys;
15+
use InteractsWithTime, RetrievesMultipleKeys;
1616

1717
/**
1818
* The Illuminate Filesystem instance.
@@ -200,6 +200,31 @@ public function forever($key, $value)
200200
return $this->put($key, $value, 0);
201201
}
202202

203+
/**
204+
* Get a lock instance.
205+
*
206+
* @param string $name
207+
* @param int $seconds
208+
* @param string|null $owner
209+
* @return \Illuminate\Contracts\Cache\Lock
210+
*/
211+
public function lock($name, $seconds = 0, $owner = null)
212+
{
213+
return new FileLock($this, $name, $seconds, $owner);
214+
}
215+
216+
/**
217+
* Restore a lock instance using the owner identifier.
218+
*
219+
* @param string $name
220+
* @param string $owner
221+
* @return \Illuminate\Contracts\Cache\Lock
222+
*/
223+
public function restoreLock($name, $owner)
224+
{
225+
return $this->lock($name, 0, $owner);
226+
}
227+
203228
/**
204229
* Remove an item from the cache.
205230
*

0 commit comments

Comments
 (0)