8000 [Cache] Use binary type for ID column for PostgreSQL by alexndlm · Pull Request #49748 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,32 +150,33 @@ protected function doFetch(array $ids): iterable
$now = time();
$expired = [];

$sql = "SELECT $this->idCol, CASE WHEN $this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > ? THEN $this->dataCol ELSE NULL END FROM $this->table WHERE $this->idCol IN (?)";
$result = $this->conn->executeQuery($sql, [
$now,
$ids,
], [
ParameterType::INTEGER,
Connection::PARAM_STR_ARRAY,
])->iterateNumeric();
$sql = str_pad('', (\count($ids) << 1) - 1, '?,');
$sql = "SELECT $this->idCol, CASE WHEN $this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > ? THEN $this->dataCol ELSE NULL END FROM $this->table WHERE $this->idCol IN ($sql)";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue($i = 1, $now, ParameterType::INTEGER);
foreach ($ids as $id) {
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$result = $stmt->executeQuery()->iterateNumeric();

foreach ($result as $row) {
$id = \is_resource($row[0]) ? stream_get_contents($row[0]) : $row[0];
if (null === $row[1]) {
$expired[] = $row[0];
$expired[] = $id;
} else {
yield $row[0] => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
yield $id => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
}
}

if ($expired) {
$sql = "DELETE FROM $this->table WHERE $this->lifetimeCol + $this->timeCol <= ? AND $this->idCol IN (?)";
$this->conn->executeStatement($sql, [
$now,
$expired,
], [
ParameterType::INTEGER,
Connection::PARAM_STR_ARRAY,
]);
$sql = str_pad('', (\count($expired) << 1) - 1, '?,');
$sql = "DELETE FROM $this->table WHERE $this->lifetimeCol + $this->timeCol <= ? AND $this->idCol IN ($sql)";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue($i = 1, $now, ParameterType::INTEGER);
foreach ($expired as $id) {
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$stmt->executeQuery();
}
}

Expand All @@ -189,7 +190,7 @@ protected function doHave(string $id): bool
$id,
time(),
], [
ParameterType::STRING,
$this->getIdColumnType(),
ParameterType::INTEGER,
]);

Expand Down Expand Up @@ -224,9 +225,16 @@ protected function doClear(string $namespace): bool
*/
protected function doDelete(array $ids): bool
{
$sql = "DELETE FROM $this->table WHERE $this->idCol IN (?)";
$ids = array_values($ids);
$sql = str_pad('', (\count($ids) << 1) - 1, '?,');
$sql = "DELETE FROM $this->table WHERE $this->idCol IN ($sql)";
try {
$this->conn->executeStatement($sql, [array_values($ids)], [Connection::PARAM_STR_ARRAY]);
$stmt = $this->conn->prepare($sql);
$i = 0;
foreach ($ids as $id) {
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$stmt->executeQuery();
} catch (TableNotFoundException $e) {
}

Expand Down Expand Up @@ -296,7 +304,7 @@ protected function doSave(array $values, int $lifetime)
$stmt->bindValue(7, $lifetime, ParameterType::INTEGER);
$stmt->bindValue(8, $now, ParameterType::INTEGER);
} elseif (null !== $platformName) {
$stmt->bindParam(1, $id);
$stmt->bindParam(1, $id, $this->getIdColumnType());
$stmt->bindParam(2, $data, ParameterType::LARGE_OBJECT);
$stmt->bindValue(3, $lifetime, ParameterType::INTEGER);
$stmt->bindValue(4, $now, ParameterType::INTEGER);
Expand Down Expand Up @@ -384,6 +392,7 @@ private function addTableToSc 8000 hema(Schema $schema): void
{
$types = [
'mysql' => 'binary',
'pgsql' => 'binary',
'sqlite' => 'text',
];

Expand All @@ -394,4 +403,9 @@ private function addTableToSchema(Schema $schema): void
$table->addColumn($this->timeCol, 'integer', ['unsigned' => true]);
$table->setPrimaryKey([$this->idCol]);
}

private function getIdColumnType(): int
{
return 'pgsql' === $this->getPlatformName() ? ParameterType::BINARY : ParameterType::STRING;
}
}
26 changes: 18 additions & 8 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function createTable()
$sql = "CREATE TABLE $this->table ($this->idCol TEXT NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
break;
case 'pgsql':
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR(255) NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
$sql = "CREATE TABLE $this->table ($this->idCol BYTEA NOT NULL PRIMARY KEY, $this->dataCol BYTEA NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
break;
case 'oci':
$sql = "CREATE TABLE $this->table ($this->idCol VARCHAR2(255) NOT NULL PRIMARY KEY, $this->dataCol BLOB NOT NULL, $this->lifetimeCol INTEGER, $this->timeCol INTEGER NOT NULL)";
Expand Down Expand Up @@ -370,7 +370,7 @@ protected function doFetch(array $ids)
$stmt = $connection->prepare($sql);
$stmt->bindValue($i = 1, $now, \PDO::PARAM_INT);
foreach ($ids as $id) {
$stmt->bindValue(++$i, $id);
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$result = $stmt->execute();

Expand All @@ -382,10 +382,11 @@ protected function doFetch(array $ids)
}

foreach ($result as $row) {
$id = \is_resource($row[0]) ? stream_get_contents($row[0]) : $row[0];
if (null === $row[1]) {
$expired[] = $row[0];
$expired[] = $id;
} else {
yield $row[0] => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
yield $id => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
}
}

Expand All @@ -395,7 +396,7 @@ protected function doFetch(array $ids)
$stmt = $connection->prepare($sql);
$stmt->bindValue($i = 1, $now, \PDO::PARAM_INT);
foreach ($expired as $id) {
$stmt->bindValue(++$i, $id);
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$stmt->execute();
}
Expand All @@ -411,7 +412,7 @@ protected function doHave(string $id)
$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = :id AND ($this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > :time)";
$stmt = $connection->prepare($sql);

$stmt->bindValue(':id', $id);
$stmt->bindValue(':id', $id, $this->getIdColumnType());
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
$stmt->execute();

Expand Down Expand Up @@ -452,7 +453,11 @@ protected function doDelete(array $ids)
$sql = "DELETE FROM $this->table WHERE $this->idCol IN ($sql)";
try {
$stmt = $this->getConnection()->prepare($sql);
$stmt->execute(array_values($ids));
$i = 0;
foreach (array_values($ids) as $id) {
$stmt->bindValue(++$i, $id, $this->getIdColumnType());
}
$stmt->execute();
} catch (\PDOException $e) {
}

Expand Down Expand Up @@ -524,7 +529,7 @@ protected function doSave(array $values, int $lifetime)
$stmt->bindValue(7, $lifetime, \PDO::PARAM_INT);
$stmt->bindValue(8, $now, \PDO::PARAM_INT);
} else {
$stmt->bindParam(':id', $id);
$stmt->bindParam(':id', $id, $this->getIdColumnType());
$stmt->bindParam(':data', $data, \PDO::PARAM_LOB);
$stmt->bindValue(':lifetime', $lifetime, \PDO::PARAM_INT);
$stmt->bindValue(':time', $now, \PDO::PARAM_INT);
Expand Down Expand Up @@ -580,4 +585,9 @@ private function getServerVersion(): string

return $this->serverVersion;
}

private function getIdColumnType(): int
{
return 'pgsql' === $this->driver ? \PDO::PARAM_LOB : \PDO::PARAM_STR;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\SkippedTestSuiteError;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\DoctrineDbalAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;

/**
* @group time-sensitive
*/
class TagAwareAdapterAndDoctrineDbalAdapterTest extends TagAwareAdapterTestCase
{
protected static $dbFile;

public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
throw new SkippedTestSuiteError('Extension pdo_sqlite required.');
}

self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
}

public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new TagAwareAdapter(new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime));
}

protected function createCacheAdapter(): AbstractAdapter
{
return new DoctrineDbalAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Cache\Tests\Adapter;

use PHPUnit\Framework\SkippedTestSuiteError;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Adapter\PdoAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;

/**
* @group time-sensitive
*/
class TagAwareAdapterAndPdoAdapterTest extends TagAwareAdapterTestCase
{
protected static $dbFile;

public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
throw new SkippedTestSuiteError('Extension pdo_sqlite required.');
}

self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
}

public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}

public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
{
return new TagAwareAdapter(new PdoAdapter('sqlite:'.self::$dbFile, '', $defaultLifetime));
}

protected function createCacheAdapter(): AbstractAdapter
{
return new PdoAdapter('sqlite:'.self::$dbFile, '', 0);
}
}
Loading
0