Skip to content

Commit

Permalink
[9.x] Fix race condition in locks issued by the file cache driver (#4…
Browse files Browse the repository at this point in the history
…6011)

* Fix file lock race condition

* wip

* revert first commit

* wip
  • Loading branch information
bytestream committed Feb 13, 2023
1 parent c72459a commit fcb6558
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Illuminate/Cache/FileLock.php
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\Cache;

class FileLock extends CacheLock
{
/**
* Attempt to acquire the lock.
*
* @return bool
*/
public function acquire()
{
return $this->store->add($this->name, $this->owner, $this->seconds);
}
}
27 changes: 26 additions & 1 deletion src/Illuminate/Cache/FileStore.php
Expand Up @@ -12,7 +12,7 @@

class FileStore implements Store, LockProvider
{
use InteractsWithTime, HasCacheLock, RetrievesMultipleKeys;
use InteractsWithTime, RetrievesMultipleKeys;

/**
* The Illuminate Filesystem instance.
Expand Down Expand Up @@ -200,6 +200,31 @@ public function forever($key, $value)
return $this->put($key, $value, 0);
}

/**
* Get a lock instance.
*
* @param string $name
* @param int $seconds
* @param string|null $owner
* @return \Illuminate\Contracts\Cache\Lock
*/
public function lock($name, $seconds = 0, $owner = null)
{
return new FileLock($this, $name, $seconds, $owner);
}

/**
* Restore a lock instance using the owner identifier.
*
* @param string $name
* @param string $owner
* @return \Illuminate\Contracts\Cache\Lock
*/
public function restoreLock($name, $owner)
{
return $this->lock($name, 0, $owner);
}

/**
* Remove an item from the cache.
*
Expand Down

0 comments on commit fcb6558

Please sign in to comment.