Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Cache/MongoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
);
Expand All @@ -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'];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Cache/MongoCacheStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@

$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]]

Check failure on line 207 in tests/Cache/MongoCacheStoreTest.php

View workflow job for this annotation

GitHub Actions / phpcs

Multi-line function calls must have a trailing comma after the last parameter.
);
$this->assertSame(10, $doc['value']);
}

public function testTTLIndex()
Expand Down
Loading