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

Skip to content

Commit 2179874

Browse files
[Cache] Use sub-second accuracy for internal expiry calculations
1 parent 4885cfe commit 2179874

File tree

10 files changed

+20
-21
lines changed

10 files changed

+20
-21
lines changed

src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,13 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
6262
$this->mergeByLifetime = \Closure::bind(
6363
function ($deferred, $namespace, &$expiredIds) use ($getId) {
6464
$byLifetime = array();
65-
$now = time();
65+
$now = microtime(true);
6666
$expiredIds = array();
6767

6868
foreach ($deferred as $key => $item) {
6969
if (null === $item->expiry) {
7070
$ttl = 0 < $item->defaultLifetime ? $item->defaultLifetime : 0;
71-
} elseif ($item->expiry > $now) {
72-
$ttl = $item->expiry - $now;
73-
} else {
71+
} elseif (0 >= $ttl = (int) ($item->expiry - $now)) {
7472
$expiredIds[] = $getId($key);
7573
continue;
7674
}

src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getItems(array $keys = array())
8787
CacheItem::validateKey($key);
8888
}
8989

90-
return $this->generateItems($keys, time(), $this->createCacheItem);
90+
return $this->generateItems($keys, microtime(true), $this->createCacheItem);
9191
}
9292

9393
/**
@@ -115,7 +115,7 @@ public function save(CacheItemInterface $item)
115115
$value = $item["\0*\0value"];
116116
$expiry = $item["\0*\0expiry"];
117117

118-
if (null !== $expiry && $expiry <= time()) {
118+
if (null !== $expiry && $expiry <= microtime(true)) {
119119
$this->deleteItem($key);
120120

121121
return true;
@@ -131,7 +131,7 @@ public function save(CacheItemInterface $item)
131131
}
132132
}
133133
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
134-
$expiry = time() + $item["\0*\0defaultLifetime"];
134+
$expiry = microtime(true) + $item["\0*\0defaultLifetime"];
135135
}
136136

137137
$this->values[$key] = $value;

src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function (CacheItemInterface $innerItem, array $item) {
6767
$item["\0*\0value"] = array(CacheItem::STATS_KEY => array($item["\0*\0value"], $stats));
6868
}
6969
$innerItem->set($item["\0*\0value"]);
70-
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U', $item["\0*\0expiry"]) : null);
70+
$innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U', (int) $item["\0*\0expiry"]) : null);
7171
},
7272
null,
7373
CacheItem::class
@@ -186,7 +186,7 @@ private function doSave(CacheItemInterface $item, $method)
186186
}
187187
$item = (array) $item;
188188
if (null === $item["\0*\0expiry"] && 0 < $item["\0*\0defaultLifetime"]) {
189-
$item["\0*\0expiry"] = time() + $item["\0*\0defaultLifetime"];
189+
$item["\0*\0expiry"] = microtime(true) + $item["\0*\0defaultLifetime"];
190190
}
191191
$innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
192192
($this->setInnerItem)($innerItem, $item);

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* added sub-second expiry accuracy for backends that support it
78
* added warmup-time stampede protection using `apcu_entry()` for locking when available
89
* added `CacheInterface` and `TaggableCacheInterface`, providing stampede protection via probabilistic early expiration
910
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ public function set($value)
8989
public function expiresAt($expiration)
9090
{
9191
if (null === $expiration) {
92-
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
92+
$this->expiry = $this->defaultLifetime > 0 ? microtime(true) + $this->defaultLifetime : null;
9393
} elseif ($expiration instanceof \DateTimeInterface) {
94-
$this->expiry = (int) $expiration->format('U');
94+
$this->expiry = (float) $expiration->format('U.u');
9595
} else {
9696
throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given', is_object($expiration) ? get_class($expiration) : gettype($expiration)));
9797
}
@@ -105,11 +105,11 @@ public function expiresAt($expiration)
105105
public function expiresAfter($time)
106106
{
107107
if (null === $time) {
108-
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
108+
$this->expiry = $this->defaultLifetime > 0 ? microtime(true) + $this->defaultLifetime : null;
109109
} elseif ($time instanceof \DateInterval) {
110-
$this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
110+
$this->expiry = microtime(true) + \DateTime::createFromFormat('U', 0)->add($time)->format('U.u');
111111
} elseif (\is_int($time)) {
112-
$this->expiry = $time + time();
112+
$this->expiry = $time + microtime(true);
113113
} else {
114114
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', is_object($time) ? get_class($time) : gettype($time)));
115115
}

src/Symfony/Component/Cache/Simple/ArrayCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function getMultiple($keys, $default = null)
6464
CacheItem::validateKey($key);
6565
}
6666

67-
return $this->generateItems($keys, time(), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
67+
return $this->generateItems($keys, microtime(true), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
6868
}
6969

7070
/**
@@ -121,7 +121,7 @@ public function setMultiple($values, $ttl = null)
121121
}
122122
}
123123
}
124-
$expiry = 0 < $ttl ? time() + $ttl : PHP_INT_MAX;
124+
$expiry = 0 < $ttl ? microtime(true) + $ttl : PHP_INT_MAX;
125125

126126
foreach ($valuesArray as $key => $value) {
127127
$this->values[$key] = $value;

src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testGetStats()
7878

7979
$expected = array(
8080
CacheItem::STATS_CTIME => 1.0,
81-
CacheItem::STATS_EXPIRY => 8.5 + time(),
81+
CacheItem::STATS_EXPIRY => 9.5 + time(),
8282
);
8383
$this->assertEquals($expected, $item->getStats(), 'Item stats should be stored alongside with its value.', 0.5);
8484
}

src/Symfony/Component/Cache/Tests/Fixtures/ArrayCache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ protected function doContains($id)
2121

2222
$expiry = $this->data[$id][1];
2323

24-
return !$expiry || time() <= $expiry || !$this->doDelete($id);
24+
return !$expiry || microtime(true) <= $expiry || !$this->doDelete($id);
2525
}
2626

2727
protected function doSave($id, $data, $lifeTime = 0)
2828
{
29-
$this->data[$id] = array($data, $lifeTime ? time() + $lifeTime : false);
29+
$this->data[$id] = array($data, $lifeTime ? microtime(true) + $lifeTime : false);
3030

3131
return true;
3232
}

src/Symfony/Component/Cache/Traits/ArrayTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function hasItem($key)
4444
{
4545
CacheItem::validateKey($key);
4646

47-
return isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
47+
return isset($this->expiries[$key]) && ($this->expiries[$key] >= microtime(true) || !$this->deleteItem($key));
4848
}
4949

5050
/**

src/Symfony/Component/Cache/Traits/GetTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
7171
function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float $startTime) {
7272
if ($item instanceof CacheItem && $startTime && $item->expiry > $endTime = microtime(true)) {
7373
$item->newStats[CacheItem::STATS_CTIME] = (float) sprintf('%.2e', $endTime - $startTime);
74-
$item->newStats[CacheItem::STATS_EXPIRY] = $item->expiry;
74+
$item->newStats[CacheItem::STATS_EXPIRY] = (float) sprintf('%.2f', $item->expiry);
7575
}
7676
$pool->save($item->set($value));
7777

0 commit comments

Comments
 (0)