Skip to content

Commit b70afc9

Browse files
committed
Simplify constructor arguments of MongoLock
1 parent c7fce86 commit b70afc9

File tree

2 files changed

+10
-23
lines changed

2 files changed

+10
-23
lines changed

src/Cache/MongoLock.php

+7-19
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ final class MongoLock extends Lock
1616
/**
1717
* Create a new lock instance.
1818
*
19-
* @param Collection $collection The MongoDB collection
20-
* @param string $name Name of the lock
21-
* @param int $seconds Time-to-live of the lock in seconds
22-
* @param string|null $owner A unique string that identifies the owner. Random if not set
23-
* @param array $lottery The prune probability odds
24-
* @param int $defaultTimeoutInSeconds The default number of seconds that a lock should be held
19+
* @param Collection $collection The MongoDB collection
20+
* @param string $name Name of the lock
21+
* @param int $seconds Time-to-live of the lock in seconds
22+
* @param string|null $owner A unique string that identifies the owner. Random if not set
23+
* @param array $lottery Probability [chance, total] of pruning expired cache items. Set to [0] to disable
2524
*/
2625
public function __construct(
2726
private readonly Collection $collection,
2827
string $name,
2928
int $seconds,
3029
?string $owner = null,
3130
private readonly array $lottery = [2, 100],
32-
private readonly int $defaultTimeoutInSeconds = 86400,
3331
) {
3432
parent::__construct($name, $seconds, $owner);
3533
}
@@ -62,7 +60,7 @@ public function acquire(): bool
6260
'expiration' => [
6361
'$cond' => [
6462
'if' => $isExpiredOrAlreadyOwned,
65-
'then' => $this->expiresAt(),
63+
'then' => $this->getUTCDateTime($this->seconds),
6664
'else' => '$expiration',
6765
],
6866
],
@@ -135,17 +133,7 @@ protected function getCurrentOwner(): ?string
135133
)['owner'] ?? null;
136134
}
137135

138-
/**
139-
* Get the UNIX timestamp indicating when the lock should expire.
140-
*/
141-
private function expiresAt(): UTCDateTime
142-
{
143-
$lockTimeout = $this->seconds > 0 ? $this->seconds : $this->defaultTimeoutInSeconds;
144-
145-
return $this->getUTCDateTime($lockTimeout);
146-
}
147-
148-
protected function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
136+
private function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
149137
{
150138
return new UTCDateTime(Carbon::now()->addSeconds($additionalSeconds));
151139
}

src/Cache/MongoStore.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class MongoStore implements LockProvider, Store
3434
* @param string $prefix Prefix for the name of cache items
3535
* @param Connection|null $lockConnection The MongoDB connection to use for the lock, if different from the cache connection
3636
* @param string $lockCollectionName Name of the collection where locks are stored
37-
* @param array{int, int} $lockLottery Probability [chance, total] of pruning expired cache items
37+
* @param array{int, int} $lockLottery Probability [chance, total] of pruning expired cache items. Set to [0] to disable
3838
* @param int $defaultLockTimeoutInSeconds Time-to-live of the locks in seconds
3939
*/
4040
public function __construct(
@@ -62,10 +62,9 @@ public function lock($name, $seconds = 0, $owner = null): MongoLock
6262
return new MongoLock(
6363
($this->lockConnection ?? $this->connection)->getCollection($this->lockCollectionName),
6464
$this->prefix . $name,
65-
$seconds,
65+
$seconds ?: $this->defaultLockTimeoutInSeconds,
6666
$owner,
6767
$this->lockLottery,
68-
$this->defaultLockTimeoutInSeconds,
6968
);
7069
}
7170

@@ -307,7 +306,7 @@ private function unserialize($value): mixed
307306
return unserialize($value);
308307
}
309308

310-
protected function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
309+
private function getUTCDateTime(int $additionalSeconds = 0): UTCDateTime
311310
{
312311
return new UTCDateTime(Carbon::now()->addSeconds($additionalSeconds));
313312
}

0 commit comments

Comments
 (0)