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
Add Cache Get and ForgetIfExpired integration
  • Loading branch information
xdevor committed Nov 17, 2023
commit 8d16c9823b6a2a7d5ac35182e399387a6fa676f9
52 changes: 52 additions & 0 deletions tests/Integration/Database/DatabaseCacheStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,60 @@ public function testAddOperationCanUpdateIfCacheExpiredInTransaction()
$this->assertSame('new-bar', $store->get('foo'));
}

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

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

$result = $store->get('foo');

$this->assertNull($result);
}

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

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

$store->get('foo');

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

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

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

$store->forgetIfExpired('foo');

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

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

$store->put('foo', 'bar', 60);

$store->forgetIfExpired('foo');

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

/**
* @return \Illuminate\Cache\DatabaseStore
*/
protected function getStore()
{
return Cache::store('database');
}

protected function getCacheTableName()
{
return config('cache.stores.database.table');
}
}