From 03a5192de993c1fb053782c7ea4345bd14dd1713 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 07:44:42 +0800 Subject: [PATCH 01/13] [10.x] Prevent Cache::get() occur race condition --- src/Illuminate/Cache/DatabaseStore.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Cache/DatabaseStore.php b/src/Illuminate/Cache/DatabaseStore.php index 17340240a88c..11e285a4fb60 100755 --- a/src/Illuminate/Cache/DatabaseStore.php +++ b/src/Illuminate/Cache/DatabaseStore.php @@ -115,7 +115,7 @@ public function get($key) // item from the cache. Then we will return a null value since the cache is // expired. We will use "Carbon" to make this comparison with the column. if ($this->currentTime() >= $cache->expiration) { - $this->forget($key); + $this->forgetIfExpired($key); return; } @@ -316,6 +316,22 @@ public function forget($key) return true; } + /** + * Remove an item from the cache if expired. + * + * @param string $key + * @return bool + */ + public function forgetIfExpired($key) + { + $this->table() + ->where('key', '=', $this->prefix.$key) + ->where('expiration', '<=', $this->getTime()) + ->delete(); + + return true; + } + /** * Remove all items from the cache. * From d7d71ba364168829fb2e9c7724216baff2937947 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 12:44:53 +0800 Subject: [PATCH 02/13] Fix test --- tests/Cache/CacheDatabaseStoreTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Cache/CacheDatabaseStoreTest.php b/tests/Cache/CacheDatabaseStoreTest.php index 48f838d4c6e6..4a2bfaa7fbf7 100755 --- a/tests/Cache/CacheDatabaseStoreTest.php +++ b/tests/Cache/CacheDatabaseStoreTest.php @@ -30,12 +30,12 @@ public function testNullIsReturnedWhenItemNotFound() public function testNullIsReturnedAndItemDeletedWhenItemIsExpired() { - $store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['forget'])->setConstructorArgs($this->getMocks())->getMock(); + $store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['forgetIfExpired'])->setConstructorArgs($this->getMocks())->getMock(); $table = m::mock(stdClass::class); $store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table); $table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table); $table->shouldReceive('first')->once()->andReturn((object) ['expiration' => 1]); - $store->expects($this->once())->method('forget')->with($this->equalTo('foo'))->willReturn(null); + $store->expects($this->once())->method('forgetIfExpired')->with($this->equalTo('foo'))->willReturn(null); $this->assertNull($store->get('foo')); } From 8d16c9823b6a2a7d5ac35182e399387a6fa676f9 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:12:23 +0800 Subject: [PATCH 03/13] Add Cache Get and ForgetIfExpired integration --- .../Database/DatabaseCacheStoreTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index 86264fb81891..a2f6b831608f 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -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'); + } } From 0048dc01b53495376a8e54f55f0a401b48c03b55 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:14:58 +0800 Subject: [PATCH 04/13] Use Cache::put() to store expired cache --- .../Database/DatabaseCacheStoreTest.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index a2f6b831608f..ae8f7c0841f9 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -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(); @@ -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(); @@ -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); @@ -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); From 7e08fe949e2a2fd361634b6473a5f54899613622 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:24:48 +0800 Subject: [PATCH 05/13] Fix test --- .../Database/DatabaseCacheStoreTest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index ae8f7c0841f9..a483dc0e368d 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -25,7 +25,7 @@ public function testPutOperationCanStoreExpired() $result = $store->put('foo', 'bar', -1); $this->assertTrue($result); - $this->assertDatabaseHas($this->getCacheTableName(), ['key' => 'foo', 'value' => 'bar']); + $this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->$this->withCachePrefix('foo'), 'value' => 'bar']); } public function testValueCanUpdateExistCache() @@ -58,7 +58,7 @@ public function testAddOperationShouldNotStoreExpired() $result = $store->add('foo', 'bar', -1); $this->assertFalse($result); - $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => 'foo', 'value' => 'bar']); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); } public function testAddOperationCanStoreNewCache() @@ -140,7 +140,7 @@ public function testGetOperationCanDeleteExpired() $store->get('foo'); - $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => 'foo']); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]); } public function testForgetIfExpiredOperationCanDeleteExpired() @@ -151,7 +151,7 @@ public function testForgetIfExpiredOperationCanDeleteExpired() $store->forgetIfExpired('foo'); - $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => 'foo']); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]); } public function testForgetIfExpiredOperationShouldNotDeleteUnExpired() @@ -162,7 +162,7 @@ public function testForgetIfExpiredOperationShouldNotDeleteUnExpired() $store->forgetIfExpired('foo'); - $this->assertDatabaseHas($this->getCacheTableName(), ['key' => 'foo']); + $this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]); } /** @@ -177,4 +177,9 @@ protected function getCacheTableName() { return config('cache.stores.database.table'); } + + protected function withCachePrefix(string $key) + { + return config('cache.prefix').$key; + } } From 985c13b1f438c0fbd746db538f790ebe792b39b1 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:12:23 +0800 Subject: [PATCH 06/13] Add Cache Get and ForgetIfExpired integration test --- .../Database/DatabaseCacheStoreTest.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index 86264fb81891..a2f6b831608f 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -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'); + } } From 2889bfc9e6e3d6d7f172e8b1d3468486fd10a28e Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:28:00 +0800 Subject: [PATCH 07/13] Test Use Cache::put() to store expired cache --- .../Database/DatabaseCacheStoreTest.php | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index a2f6b831608f..ae8f7c0841f9 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -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(); @@ -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(); @@ -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); @@ -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); From 043fa6da9c1f2ced1a20fd46b8ca22f13765c931 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 13:31:06 +0800 Subject: [PATCH 08/13] Fix wrong test statement --- tests/Integration/Database/DatabaseCacheStoreTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index a483dc0e368d..841a005225b9 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -25,7 +25,7 @@ public function testPutOperationCanStoreExpired() $result = $store->put('foo', 'bar', -1); $this->assertTrue($result); - $this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->$this->withCachePrefix('foo'), 'value' => 'bar']); + $this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); } public function testValueCanUpdateExistCache() From 88f694480843d86a434aca70ba9ce569b5229b58 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 18:56:56 +0800 Subject: [PATCH 09/13] Fix test --- .../Database/DatabaseCacheStoreTest.php | 51 ++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index 841a005225b9..ca5e8d8c033e 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -18,14 +18,14 @@ public function testValueCanStoreNewCache() $this->assertSame('bar', $store->get('foo')); } - public function testPutOperationCanStoreExpired() + public function testPutOperationShouldNotStoreExpired() { $store = $this->getStore(); $result = $store->put('foo', 'bar', -1); - $this->assertTrue($result); - $this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); + $this->assertFalse($result); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); } public function testValueCanUpdateExistCache() @@ -100,7 +100,13 @@ public function testAddOperationCanUpdateIfCacheExpired() { $store = $this->getStore(); - $store->put('foo', 'bar', -1); + $this->insertToCacheTable( + [ + 'key' => $this->withCachePrefix('foo'), + 'value' => 'bar', + 'expiration' => 0 + ] + ); $result = $store->add('foo', 'new-bar', 60); $this->assertTrue($result); @@ -111,7 +117,13 @@ public function testAddOperationCanUpdateIfCacheExpiredInTransaction() { $store = $this->getStore(); - $store->put('foo', 'bar', -1); + $this->insertToCacheTable( + [ + 'key' => $this->withCachePrefix('foo'), + 'value' => 'bar', + 'expiration' => 0 + ] + ); DB::beginTransaction(); $result = $store->add('foo', 'new-bar', 60); @@ -125,7 +137,13 @@ public function testGetOperationReturnNullIfExpired() { $store = $this->getStore(); - $store->put('foo', 'bar', -1); + $this->insertToCacheTable( + [ + 'key' => $this->withCachePrefix('foo'), + 'value' => 'bar', + 'expiration' => 0 + ] + ); $result = $store->get('foo'); @@ -136,7 +154,13 @@ public function testGetOperationCanDeleteExpired() { $store = $this->getStore(); - $store->put('foo', 'bar', -1); + $this->insertToCacheTable( + [ + 'key' => $this->withCachePrefix('foo'), + 'value' => 'bar', + 'expiration' => 0 + ] + ); $store->get('foo'); @@ -147,7 +171,13 @@ public function testForgetIfExpiredOperationCanDeleteExpired() { $store = $this->getStore(); - $store->put('foo', 'bar', -1); + $this->insertToCacheTable( + [ + 'key' => $this->withCachePrefix('foo'), + 'value' => 'bar', + 'expiration' => 0 + ] + ); $store->forgetIfExpired('foo'); @@ -182,4 +212,9 @@ protected function withCachePrefix(string $key) { return config('cache.prefix').$key; } + + protected function insertToCacheTable(array $data) + { + DB::table($this->getCacheTableName())->insert($data); + } } From bff6cf5816fb91031648228812fa23c95c17dc40 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 19:11:44 +0800 Subject: [PATCH 10/13] Fix test --- .../Database/DatabaseCacheStoreTest.php | 61 ++++++------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index ca5e8d8c033e..f1f888a1cfe4 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Database; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Orchestra\Testbench\Attributes\WithMigration; @@ -22,10 +23,9 @@ public function testPutOperationShouldNotStoreExpired() { $store = $this->getStore(); - $result = $store->put('foo', 'bar', -1); + $store->put('foo', 'bar', 0); - $this->assertFalse($result); - $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]); } public function testValueCanUpdateExistCache() @@ -55,10 +55,10 @@ public function testAddOperationShouldNotStoreExpired() { $store = $this->getStore(); - $result = $store->add('foo', 'bar', -1); + $result = $store->add('foo', 'bar', 0); $this->assertFalse($result); - $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo'), 'value' => 'bar']); + $this->assertDatabaseMissing($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]); } public function testAddOperationCanStoreNewCache() @@ -100,13 +100,7 @@ public function testAddOperationCanUpdateIfCacheExpired() { $store = $this->getStore(); - $this->insertToCacheTable( - [ - 'key' => $this->withCachePrefix('foo'), - 'value' => 'bar', - 'expiration' => 0 - ] - ); + $this->insertToCacheTable('foo', 'bar', 0); $result = $store->add('foo', 'new-bar', 60); $this->assertTrue($result); @@ -117,13 +111,7 @@ public function testAddOperationCanUpdateIfCacheExpiredInTransaction() { $store = $this->getStore(); - $this->insertToCacheTable( - [ - 'key' => $this->withCachePrefix('foo'), - 'value' => 'bar', - 'expiration' => 0 - ] - ); + $this->insertToCacheTable('foo', 'bar', 0); DB::beginTransaction(); $result = $store->add('foo', 'new-bar', 60); @@ -137,13 +125,7 @@ public function testGetOperationReturnNullIfExpired() { $store = $this->getStore(); - $this->insertToCacheTable( - [ - 'key' => $this->withCachePrefix('foo'), - 'value' => 'bar', - 'expiration' => 0 - ] - ); + $this->insertToCacheTable('foo', 'bar', 0); $result = $store->get('foo'); @@ -154,13 +136,7 @@ public function testGetOperationCanDeleteExpired() { $store = $this->getStore(); - $this->insertToCacheTable( - [ - 'key' => $this->withCachePrefix('foo'), - 'value' => 'bar', - 'expiration' => 0 - ] - ); + $this->insertToCacheTable('foo', 'bar', 0); $store->get('foo'); @@ -171,13 +147,7 @@ public function testForgetIfExpiredOperationCanDeleteExpired() { $store = $this->getStore(); - $this->insertToCacheTable( - [ - 'key' => $this->withCachePrefix('foo'), - 'value' => 'bar', - 'expiration' => 0 - ] - ); + $this->insertToCacheTable('foo', 'bar', 0); $store->forgetIfExpired('foo'); @@ -213,8 +183,15 @@ protected function withCachePrefix(string $key) return config('cache.prefix').$key; } - protected function insertToCacheTable(array $data) + protected function insertToCacheTable(string $key, $value, $ttl = 60) { - DB::table($this->getCacheTableName())->insert($data); + DB::table($this->getCacheTableName()) + ->insert( + [ + 'key' => $this->withCachePrefix($key), + 'value' => $value, + 'expiration' => Carbon::now()->addSeconds($ttl)->getTimestamp() + ] + ); } } From 4cdb94b18fe66f8d52bc50aed4cda0a4e8cc1231 Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 19:42:23 +0800 Subject: [PATCH 11/13] [10.x] Fix DatabaseStore `add()` failed due to incorrect key --- src/Illuminate/Cache/DatabaseStore.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Cache/DatabaseStore.php b/src/Illuminate/Cache/DatabaseStore.php index 11e285a4fb60..3e016af2784b 100755 --- a/src/Illuminate/Cache/DatabaseStore.php +++ b/src/Illuminate/Cache/DatabaseStore.php @@ -150,11 +150,12 @@ public function put($key, $value, $seconds) */ public function add($key, $value, $seconds) { + $noPrefixKey = $key; $key = $this->prefix.$key; $value = $this->serialize($value); $expiration = $this->getTime() + $seconds; - if (! is_null($this->get($key))) { + if (! is_null($this->get($noPrefixKey))) { return false; } From 204c515c1780d637cf8fdf8fb2217701052071fb Mon Sep 17 00:00:00 2001 From: xdevor <0.yu.zhuang@gmail.com> Date: Fri, 17 Nov 2023 19:51:19 +0800 Subject: [PATCH 12/13] Style fix --- tests/Integration/Database/DatabaseCacheStoreTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/DatabaseCacheStoreTest.php b/tests/Integration/Database/DatabaseCacheStoreTest.php index f1f888a1cfe4..f9d7d440ab9a 100644 --- a/tests/Integration/Database/DatabaseCacheStoreTest.php +++ b/tests/Integration/Database/DatabaseCacheStoreTest.php @@ -190,7 +190,7 @@ protected function insertToCacheTable(string $key, $value, $ttl = 60) [ 'key' => $this->withCachePrefix($key), 'value' => $value, - 'expiration' => Carbon::now()->addSeconds($ttl)->getTimestamp() + 'expiration' => Carbon::now()->addSeconds($ttl)->getTimestamp(), ] ); } From 604bb7be04ac7adc82ec920995aacac9ea8d0605 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 17 Nov 2023 08:50:29 -0600 Subject: [PATCH 13/13] formatting --- src/Illuminate/Cache/DatabaseStore.php | 11 +++++------ src/Illuminate/Support/Number.php | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Cache/DatabaseStore.php b/src/Illuminate/Cache/DatabaseStore.php index 3e016af2784b..e5b72107ce96 100755 --- a/src/Illuminate/Cache/DatabaseStore.php +++ b/src/Illuminate/Cache/DatabaseStore.php @@ -150,15 +150,14 @@ public function put($key, $value, $seconds) */ public function add($key, $value, $seconds) { - $noPrefixKey = $key; + if (! is_null($this->get($key))) { + return false; + } + $key = $this->prefix.$key; $value = $this->serialize($value); $expiration = $this->getTime() + $seconds; - if (! is_null($this->get($noPrefixKey))) { - return false; - } - $doesntSupportInsertOrIgnore = [SqlServerConnection::class]; if (! in_array(get_class($this->getConnection()), $doesntSupportInsertOrIgnore)) { @@ -318,7 +317,7 @@ public function forget($key) } /** - * Remove an item from the cache if expired. + * Remove an item from the cache if it is expired. * * @param string $key * @return bool diff --git a/src/Illuminate/Support/Number.php b/src/Illuminate/Support/Number.php index f86e681ecb0a..0fb1c6782b59 100644 --- a/src/Illuminate/Support/Number.php +++ b/src/Illuminate/Support/Number.php @@ -117,7 +117,7 @@ public static function forHumans(int|float $number, int $precision = 0) $displayExponent = $numberExponent - ($numberExponent % 3); $number /= pow(10, $displayExponent); - return trim(sprintf('%s %s', number_format($number, $precision), $units[$displayExponent])); + return trim(sprintf('%s %s', number_format($number, $precision), $units[$displayExponent] ?? '')); } /**