From 5beb0b7fa302d52f923b57c71d5a2dbcd170db88 Mon Sep 17 00:00:00 2001 From: zigzagdev Date: Mon, 15 Dec 2025 10:44:54 +0900 Subject: [PATCH 1/2] fix MongoStore:: increment to ignore expired cache entries --- src/Cache/MongoStore.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Cache/MongoStore.php b/src/Cache/MongoStore.php index e37884a93..6720ec3c4 100644 --- a/src/Cache/MongoStore.php +++ b/src/Cache/MongoStore.php @@ -182,14 +182,18 @@ public function get($key): mixed #[Override] public function increment($key, $value = 1): int|float|false { + $now = $this->getUTCDateTime(); + $result = $this->collection->findOneAndUpdate( [ '_id' => $this->prefix . $key, + 'expires_at' => ['$gt' => $now], ], [ '$inc' => ['value' => $value], ], [ + 'projection' => ['value' => 1, 'expires_at' => 1], 'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER, ], ); @@ -198,12 +202,6 @@ public function increment($key, $value = 1): int|float|false return false; } - if ($result['expires_at'] <= $this->getUTCDateTime()) { - $this->forgetIfExpired($key); - - return false; - } - return $result['value']; } From d0c00a286e4de059195238ddfa195d42aab1bb61 Mon Sep 17 00:00:00 2001 From: zigzagdev Date: Mon, 15 Dec 2025 10:45:48 +0900 Subject: [PATCH 2/2] add: regression test for increment method about expired cache --- tests/Cache/MongoCacheStoreTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Cache/MongoCacheStoreTest.php b/tests/Cache/MongoCacheStoreTest.php index 6f4ee79f4..5dd73c2aa 100644 --- a/tests/Cache/MongoCacheStoreTest.php +++ b/tests/Cache/MongoCacheStoreTest.php @@ -199,6 +199,14 @@ public function testIncrementDecrement() $this->insertToCacheTable('foo', 10, -5); $this->assertFalse($store->increment('foo', 5)); + + $doc = DB::connection('mongodb') + ->getCollection($this->getCacheCollectionName()) + ->findOne( + ['_id' => $this->withCachePrefix('foo')], + ['projection' => ['value' => 1]] + ); + $this->assertSame(10, $doc['value']); } public function testTTLIndex()