Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix race condition in locks issued by the file cache driver #46011

Merged
merged 4 commits into from Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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