8000 feature #27694 [FrameworkBundle][Cache] Allow configuring PDO-based c… · symfony/symfony@cbda6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit cbda6a3

Browse files
committed
feature #27694 [FrameworkBundle][Cache] Allow configuring PDO-based cache pools, with table auto-creation on first use (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [FrameworkBundle][Cache] Allow configuring PDO-based cache pools, with table auto-creation on first use | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - * Allowed configuring PDO-based cache pools via a new `cache.adapter.pdo` abstract service * added automatic table creation when using Doctrine DBAL with PDO-based backends Commits ------- 1484117 [FrameworkBundle][Cache] Allow configuring PDO-based cache pools, with table auto-creation on first use
2 parents 254f4c8 + 1484117 commit cbda6a3

File tree

9 files changed

+43
-8
lines changed

9 files changed

+43
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

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

77
* Allowed configuring taggable cache pools via a new `framework.cache.pools.tags` option (bool|service-id)
8+
* Allowed configuring PDO-based cache pools via a new `cache.adapter.pdo` abstract service
89
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead
910
* Deprecated processing of services tagged `security.expression_language_provider` in favor of a new `AddExpressionLanguageProvidersPass` in SecurityBundle.
1011

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
867867
->scalarNode('default_psr6_provider')->end()
868868
->scalarNode('default_redis_provider')->defaultValue('redis://localhost')->end()
869869
->scalarNode('default_memcached_provider')->defaultValue('memcached://localhost')->end()
870+
->scalarNode('default_pdo_provider')->defaultValue('doctrine.dbal.default_connection')->end()
870871
->arrayNode('pools')
871872
->useAttributeAsKey('name')
872873
->prototype('array')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
15531553
// Inline any env vars referenced in the parameter
15541554
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
15551555
}
1556-
foreach (array('doctrine', 'psr6', 'redis', 'memcached') as $name) {
1556+
foreach (array('doctrine', 'psr6', 'redis', 'memcached', 'pdo') as $name) {
15571557
if (isset($config[$name = 'default_'.$name.'_provider'])) {
15581558
$container->setAlias('cache.'.$name, new Alias(Compiler\CachePoolPass::getServiceProvider($container, $config[$name]), false));
15591559
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@
109109
</call>
110110
</service>
111111

112+
<service id="cache.adapter.pdo" class="Symfony\Component\Cache\Adapter\PdoAdapter" abstract="true">
113+
<tag name="cache.pool" provider="cache.default_pdo_provider" clearer="cache.default_clearer" reset="reset" />
114+
<tag name="monolog.logger" channel="cache" />
115+
<argument /> <!-- PDO connection service -->
116+
<argument /> <!-- namespace -->
117+
<argument>0</argument> <!-- default lifetime -->
118+
<argument type="collection" /> <!-- table options -->
119+
<argument type="service" id="cache.default_marshaller" on-invalid="ignore" />
120+
<call method="setLogger">
121+
<argument type="service" id="logger" on-invalid="ignore" />
122+
</call>
123+
</service>
124+
112125
<service id="cache.adapter.array" class="Symfony\Component\Cache\Adapter\ArrayAdapter" abstract="true">
113126
<tag name="cache.pool" clearer="cache.default_clearer" />
114127
<tag name="monolog.logger" channel="cache" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ protected static function getBundleDefaultConfig()
267267
'directory' => '%kernel.cache_dir%/pools',
268268
'default_redis_provider' => 'redis://localhost',
269269
'default_memcached_provider' => 'memcached://localhost',
270+
'default_pdo_provider' => 'doctrine.dbal.default_connection',
270271
),
271272
'workflows' => array(
272273
'enabled' => false,

src/Symfony/Component/Cache/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added `CacheInterface`, which provides stampede protection via probabilistic early expiration and should become the preferred way to use a cache
88
* added sub-second expiry accuracy for backends that support it
99
* added support for phpredis 4 `compression` and `tcp_keepalive` options
10+
* added automatic table creation when using Doctrine DBAL with PDO-based backends
1011
* throw `LogicException` when `CacheItem::tag()` is called on an item coming from a non tag-aware pool
1112
* deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead
1213
* deprecated the `AbstractAdapter::createSystemCache()` method

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static function setupBeforeClass()
3333
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
3434

3535
$pool = new PdoAdapter(DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => self::$dbFile)));
36-
$pool->createTable();
3736
}
3837

3938
public static function tearDownAfterClass()

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1616
use Doctrine\DBAL\DBALException;
17+
use Doctrine\DBAL\Exception\TableNotFoundException;
1718
use Doctrine\DBAL\Schema\Schema;
1819
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1920

@@ -150,7 +151,11 @@ public function prune()
150151
$deleteSql .= " AND $this->idCol LIKE :namespace";
151152
}
152153

153-
$delete = $this->getConnection()->prepare($deleteSql);
154+
try {
155+
$delete = $this->getConnection()->prepare($deleteSql);
156+
} catch (TableNotFoundException $e) {
157+
return true;
158+
}
154159
$delete->bindValue(':time', time(), \PDO::PARAM_INT);
155160

156161
if ('' !== $this->namespace) {
@@ -229,7 +234,10 @@ protected function doClear($namespace)
229234
$sql = "DELETE FROM $this->table WHERE $this->idCol LIKE '$namespace%'";
230235
}
231236

232-
$conn->exec($sql);
237+
try {
238+
$conn->exec($sql);
239+
} catch (TableNotFoundException $e) {
240+
}
233241

234242
return true;
235243
}
@@ -241,8 +249,11 @@ protected function doDelete(array $ids)
241249
{
242250
$sql = str_pad('', (count($ids) << 1) - 1, '?,');
243251
$sql = "DELETE FROM $this->table WHERE $this->idCol IN ($sql)";
244-
$stmt = $this->getConnection()->prepare($sql);
245-
$stmt->execute(array_values($ids));
252+
try {
253+
$stmt = $this->getConnection()->prepare($sql);
254+
$stmt->execute(array_values($ids));
255+
} catch (TableNotFoundException $e) {
256+
}
246257

247258
return true;
248259
}
@@ -302,7 +313,14 @@ protected function doSave(array $values, $lifetime)
302313

303314
$now = time();
304315
$lifetime = $lifetime ?: null;
305-
$stmt = $conn->prepare($sql);
316+
try {
317+
$stmt = $conn->prepare($sql);
318+
} catch (TableNotFoundException $e) {
319+
if (!$conn->isTransactionActive() || \in_array($this->driver, array('pgsql', 'sqlite', 'sqlsrv'), true)) {
320+
$this->createTable();
321+
}
322+
$stmt = $conn->prepare($sql);
323+
}
306324

307325
if ('sqlsrv' === $driver || 'oci' === $driver) {
308326
$stmt->bindParam(1, $id);

src/Symfony/Component/Cache/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
"require-dev": {
2929
"cache/integration-tests": "dev-master",
3030
"doctrine/cache": "~1.6",
31-
"doctrine/dbal": "~2.4",
31+
"doctrine/dbal": "~2.5",
3232
"predis/predis": "~1.0",
3333
"symfony/var-dumper": "^4.1.1"
3434
},
3535
"conflict": {
36+
"doctrine/dbal": "<2.5",
3637
"symfony/var-dumper": "<3.4"
3738
},
3839
"autoload": {

0 commit comments

Comments
 (0)
0