Skip to content

Commit 95afc67

Browse files
bug symfony#18571 [Cache] Minor tweaks (nicolas-grekas)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Cache] Minor tweaks | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | php-cache/integration-tests#51 | License | MIT | Doc PR | - Commits ------- 9dcbe34 [Cache] Minor tweaks
2 parents 47cb0c3 + 9dcbe34 commit 95afc67

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
4646
CacheItem::class
4747
);
4848
$this->mergeByLifetime = \Closure::bind(
49-
function ($deferred, $namespace) {
49+
function ($deferred, $namespace, &$expiredIds) {
5050
$byLifetime = array();
5151
$now = time();
52+
$expiredIds = array();
5253

5354
foreach ($deferred as $key => $item) {
5455
if (null === $item->expiry) {
5556
$byLifetime[0][$namespace.$key] = $item->value;
5657
} elseif ($item->expiry > $now) {
5758
$byLifetime[$item->expiry - $now][$namespace.$key] = $item->value;
59+
} else {
60+
$expiredIds[] = $namespace.$key;
5861
}
5962
}
6063

@@ -277,9 +280,12 @@ public function commit()
277280
{
278281
$ok = true;
279282
$byLifetime = $this->mergeByLifetime;
280-
$byLifetime = $byLifetime($this->deferred, $this->namespace);
283+
$byLifetime = $byLifetime($this->deferred, $this->namespace, $expiredIds);
281284
$retry = $this->deferred = array();
282285

286+
if ($expiredIds) {
287+
$this->doDelete($expiredIds);
288+
}
283289
foreach ($byLifetime as $lifetime => $values) {
284290
try {
285291
$e = $this->doSave($values, $lifetime);

src/Symfony/Component/Cache/CacheItem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function expiresAt($expiration)
7373
if (null === $expiration) {
7474
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
7575
} elseif ($expiration instanceof \DateTimeInterface) {
76-
$this->expiry = $expiration->format('U');
76+
$this->expiry = (int) $expiration->format('U');
7777
} else {
7878
throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given', is_object($expiration) ? get_class($expiration) : gettype($expiration)));
7979
}
@@ -89,7 +89,7 @@ public function expiresAfter($time)
8989
if (null === $time) {
9090
$this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
9191
} elseif ($time instanceof \DateInterval) {
92-
$this->expiry = \DateTime::createFromFormat('U', time())->add($time)->format('U');
92+
$this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
9393
} elseif (is_int($time)) {
9494
$this->expiry = $time + time();
9595
} else {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,28 @@ public function createCachePool()
2727

2828
return new FilesystemAdapter('sf-cache');
2929
}
30+
31+
public static function tearDownAfterClass()
32+
{
33+
self::rmdir(sys_get_temp_dir().'/symfony-cache');
34+
}
35+
36+
public static function rmdir($dir)
37+
{
38+
if (!$dir || 0 !== strpos(dirname($dir), sys_get_temp_dir())) {
39+
throw new \Exception(__METHOD__."() operates only on subdirs of system's temp dir");
40+
}
41+
$children = new \RecursiveIteratorIterator(
42+
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS),
43+
\RecursiveIteratorIterator::CHILD_FIRST
44+
);
45+
foreach($children as $child) {
46+
if ($child->isDir()){
47+
rmdir($child);
48+
} else {
49+
unlink($child);
50+
}
51+
}
52+
rmdir($dir);
53+
}
3054
}

0 commit comments

Comments
 (0)