Skip to content

Commit 07c38df

Browse files
committed
bug symfony#23326 [Cache] fix cleanup of expired items for PdoAdapter (dmaicher)
This PR was merged into the 3.2 branch. Discussion ---------- [Cache] fix cleanup of expired items for PdoAdapter | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#23313 | License | MIT | Doc PR | - This fixes the query being executed to cleanup expired items from the `cache_items` table using the `PdoAdapter`. I also added a test case to make sure we do the cleanup successfully. Commits ------- c183b0e [Cache] fix cleanup of expired items for PdoAdapter
2 parents 25ab339 + c183b0e commit 07c38df

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected function doFetch(array $ids)
195195
foreach ($expired as $id) {
196196
$stmt->bindValue(++$i, $id);
197197
}
198-
$stmt->execute($expired);
198+
$stmt->execute();
199199
}
200200
}
201201

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,30 @@ public function createCachePool($defaultLifetime = 0)
4141
{
4242
return new PdoAdapter('sqlite:'.self::$dbFile, 'ns', $defaultLifetime);
4343
}
44+
45+
public function testCleanupExpiredItems()
46+
{
47+
$pdo = new \PDO('sqlite:'.self::$dbFile);
48+
49+
$getCacheItemCount = function () use ($pdo) {
50+
return (int) $pdo->query('SELECT COUNT(*) FROM cache_items')->fetch(\PDO::FETCH_COLUMN);
51+
};
52+
53+
$this->assertSame(0, $getCacheItemCount());
54+
55+
$cache = $this->createCachePool();
56+
57+
$item = $cache->getItem('some_nice_key');
58+
$item->expiresAfter(1);
59+
$item->set(1);
60+
61+
$cache->save($item);
62+
$this->assertSame(1, $getCacheItemCount());
63+
64+
sleep(2);
65+
66+
$newItem = $cache->getItem($item->getKey());
67+
$this->assertFalse($newItem->isHit());
68+
$this->assertSame(0, $getCacheItemCount(), 'PDOAdapter must clean up expired items');
69+
}
4470
}

0 commit comments

Comments
 (0)