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

[10.x] Prevent DB Cache::get() occur race condition #49031

Merged
Prev Previous commit
Next Next commit
Use Cache::put() to store expired cache
  • Loading branch information
xdevor committed Nov 17, 2023
commit 0048dc01b53495376a8e54f55f0a401b48c03b55
24 changes: 22 additions & 2 deletions tests/Integration/Database/DatabaseCacheStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public function testValueCanStoreNewCache()
$this->assertSame('bar', $store->get('foo'));
}

public function testPutOperationCanStoreExpired()
{
$store = $this->getStore();

$result = $store->put('foo', 'bar', -1);

$this->assertTrue($result);
$this->assertDatabaseHas($this->getCacheTableName(), ['key' => 'foo', 'value' => 'bar']);
}

public function testValueCanUpdateExistCache()
{
$store = $this->getStore();
Expand All @@ -41,6 +51,16 @@ public function testValueCanUpdateExistCacheInTransaction()
$this->assertSame('new-bar', $store->get('foo'));
}

public function testAddOperationShouldNotStoreExpired()
{
$store = $this->getStore();

$result = $store->add('foo', 'bar', -1);

$this->assertFalse($result);
$this->assertDatabaseMissing($this->getCacheTableName(), ['key' => 'foo', 'value' => 'bar']);
}

public function testAddOperationCanStoreNewCache()
{
$store = $this->getStore();
Expand Down Expand Up @@ -80,7 +100,7 @@ public function testAddOperationCanUpdateIfCacheExpired()
{
$store = $this->getStore();

$store->add('foo', 'bar', 0);
$store->put('foo', 'bar', -1);
$result = $store->add('foo', 'new-bar', 60);

$this->assertTrue($result);
Expand All @@ -91,7 +111,7 @@ public function testAddOperationCanUpdateIfCacheExpiredInTransaction()
{
$store = $this->getStore();

$store->add('foo', 'bar', 0);
$store->put('foo', 'bar', -1);

DB::beginTransaction();
$result = $store->add('foo', 'new-bar', 60);
Expand Down
Loading