Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b6be16

Browse files
committed
Use lifetime instead of ExpiringDate
1 parent 9743bfb commit 3b6be16

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

src/Symfony/Component/Lock/Key.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function getState($stateKey)
7272
return $this->state[$stateKey];
7373
}
7474

75+
public function resetLifetime()
76+
{
77+
$this->expiringTime = null;
78+
}
79+
7580
/**
7681
* @param float $ttl The expiration delay of locks in seconds.
7782
*/
@@ -84,17 +89,14 @@ public function reduceLifetime($ttl)
8489
}
8590
}
8691

87-
public function resetExpiringDate()
88-
{
89-
$this->expiringTime = null;
90-
}
91-
9292
/**
93-
* @return \DateTimeImmutable
93+
* Returns the remaining lifetime in seconds.
94+
*
95+
* @return float|null
9496
*/
95-
public function getExpiringDate()
97+
public function getRemainingLifetime()
9698
{
97-
return \DateTimeImmutable::createFromFormat('U.u', (string) $this->expiringTime);
99+
return null === $this->expiringTime ? null : $this->expiringTime - microtime(true);
98100
}
99101

100102
/**

src/Symfony/Component/Lock/Lock.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function refresh()
9494
}
9595

9696
try {
97-
$this->key->resetExpiringDate();
97+
$this->key->resetLifetime();
9898
$this->store->putOffExpiration($this->key, $this->ttl);
9999

100100
if ($this->key->isExpired()) {
@@ -140,8 +140,13 @@ public function isExpired()
140140
return $this->key->isExpired();
141141
}
142142

143-
public function getExpiringDate()
143+
/**
144+
* Returns the remaining lifetime in seconds.
145+
*
146+
* @return float|null
147+
*/
148+
public function getRemainingLifetime()
144149
{
145-
return $this->key->getExpiringDate();
150+
return $this->key->getRemainingLifetime();
146151
}
147152
}

src/Symfony/Component/Lock/Tests/LockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function testExpiration($ttls, $expected)
165165

166166
foreach ($ttls as $ttl) {
167167
if (null === $ttl) {
168-
$key->resetExpiringDate();
168+
$key->resetLifetime();
169169
} else {
170170
$key->reduceLifetime($ttl);
171171
}

src/Symfony/Component/Lock/Tests/Store/ExpiringStoreTestTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function testSetExpiration()
101101

102102
$store->save($key);
103103
$store->putOffExpiration($key, 1);
104-
$this->assertNotNull($key->getExpiringDate());
104+
$this->assertGreaterThanOrEqual(0, $key->getRemainingLifetime());
105+
$this->assertLessThanOrEqual(1, $key->getRemainingLifetime());
105106
}
106107
}

0 commit comments

Comments
 (0)