-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[7.x] Cache::lock support for the database cache driver #32639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6e6f54d
work on database lock support
taylorotwell 1f8c8bd
Apply fixes from StyleCI (#32640)
taylorotwell d236168
Update CacheManager.php
taylorotwell 3b06a5c
Fixed typo
GrahamCampbell 8385c0b
Update DatabaseStore.php
GrahamCampbell 62a635d
add tests
taylorotwell 54d27d6
add fiel
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace Illuminate\Cache; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\QueryException; | ||
|
||
class DatabaseLock extends Lock | ||
{ | ||
/** | ||
* The database connection instance. | ||
* | ||
* @var \Illuminate\Database\Connection | ||
*/ | ||
protected $connection; | ||
|
||
/** | ||
* The database table name. | ||
* | ||
* @var string | ||
*/ | ||
protected $table; | ||
|
||
/** | ||
* The prune probability odds. | ||
* | ||
* @var array | ||
*/ | ||
protected $lottery; | ||
|
||
/** | ||
* Create a new lock instance. | ||
* | ||
* @param \Illuminate\Database\Connection $connection | ||
* @param string $table | ||
* @param string $name | ||
* @param int $seconds | ||
* @param string|null $owner | ||
* @param array $lottery | ||
* @return void | ||
*/ | ||
public function __construct(Connection $connection, $table, $name, $seconds, $owner = null, $lottery = [2, 100]) | ||
{ | ||
parent::__construct($name, $seconds, $owner); | ||
|
||
$this->connection = $connection; | ||
$this->table = $table; | ||
$this->lottery = $lottery; | ||
} | ||
|
||
/** | ||
* Attempt to acquire the lock. | ||
* | ||
* @return bool | ||
*/ | ||
public function acquire() | ||
{ | ||
$acquired = false; | ||
|
||
try { | ||
$this->connection->table($this->table)->insert([ | ||
'id' => $this->name, | ||
'owner' => $this->owner, | ||
'expires_at' => $this->expiresAt(), | ||
]); | ||
|
||
$acquired = true; | ||
} catch (QueryException $e) { | ||
$updated = $this->connection->table($this->table) | ||
->where('id', $this->name) | ||
->where(function ($query) { | ||
return $query->where('owner', $this->owner)->orWhere('expires_at', '<=', time()); | ||
})->update([ | ||
'owner' => $this->owner, | ||
'expires_at' => $this->expiresAt(), | ||
]); | ||
|
||
$acquired = $updated >= 1; | ||
} | ||
|
||
if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) { | ||
$this->connection->table($this->table)->where('expires_at', '<=', time())->delete(); | ||
} | ||
|
||
return $acquired; | ||
} | ||
|
||
/** | ||
* Get the UNIX timestamp indicating when the lock should expire. | ||
* | ||
* @return int | ||
*/ | ||
protected function expiresAt() | ||
{ | ||
return $this->seconds > 0 ? time() + $this->seconds : now()->addDays(1)->getTimestamp(); | ||
} | ||
|
||
/** | ||
* Release the lock. | ||
* | ||
* @return bool | ||
*/ | ||
public function release() | ||
{ | ||
if ($this->isOwnedByCurrentProcess()) { | ||
$this->connection->table($this->table) | ||
->where('id', $this->name) | ||
->where('owner', $this->owner) | ||
->delete(); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Releases this lock in disregard of ownership. | ||
* | ||
* @return void | ||
*/ | ||
public function forceRelease() | ||
{ | ||
$this->connection->table($this->table) | ||
->where('id', $this->name) | ||
->delete(); | ||
} | ||
|
||
/** | ||
* Returns the owner value written into the driver for this lock. | ||
* | ||
* @return string | ||
*/ | ||
protected function getCurrentOwner() | ||
{ | ||
return optional($this->connection->table($this->table)->where('id', $this->name)->first())->owner; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Database; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class DatabaseLockTest extends DatabaseTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Schema::create('cache_locks', function (Blueprint $table) { | ||
$table->string('id')->primary(); | ||
$table->string('owner'); | ||
$table->integer('expires_at'); | ||
}); | ||
} | ||
|
||
public function testLockCanBeAcquired() | ||
{ | ||
$lock = Cache::driver('database')->lock('foo'); | ||
$this->assertTrue($lock->get()); | ||
|
||
$otherLock = Cache::driver('database')->lock('foo'); | ||
$this->assertFalse($otherLock->get()); | ||
|
||
$lock->release(); | ||
|
||
$otherLock = Cache::driver('database')->lock('foo'); | ||
$this->assertTrue($otherLock->get()); | ||
|
||
$otherLock->release(); | ||
} | ||
|
||
public function testLockCanBeForceReleased() | ||
{ | ||
$lock = Cache::driver('database')->lock('foo'); | ||
$this->assertTrue($lock->get()); | ||
|
||
$otherLock = Cache::driver('database')->lock('foo'); | ||
$otherLock->forceRelease(); | ||
$this->assertTrue($otherLock->get()); | ||
|
||
$otherLock->release(); | ||
} | ||
|
||
public function testExpiredLockCanBeRetrieved() | ||
{ | ||
$lock = Cache::driver('database')->lock('foo'); | ||
$this->assertTrue($lock->get()); | ||
DB::table('cache_locks')->update(['expires_at' => now()->subDays(1)->getTimestamp()]); | ||
|
||
$otherLock = Cache::driver('database')->lock('foo'); | ||
$this->assertTrue($otherLock->get()); | ||
|
||
$otherLock->release(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.