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

Skip to content

[Cache] Allow tag cache to work with PostgreSQL #49741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
27 changes: 17 additions & 10 deletions src/Symfony/Component/Cache/Adapter/DoctrineDbalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function doFetch(array $ids): iterable
$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,
$this->encodeIds($ids),
], [
ParameterType::INTEGER,
Connection::PARAM_STR_ARRAY,
Expand All @@ -163,7 +163,7 @@ protected function doFetch(array $ids): iterable
if (null === $row[1]) {
$expired[] = $row[0];
} else {
yield $row[0] => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
yield $this->decodeIds($row[0]) => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
}
}

Expand All @@ -186,7 +186,7 @@ protected function doHave(string $id): bool
{
$sql = "SELECT 1 FROM $this->table WHERE $this->idCol = ? AND ($this->lifetimeCol IS NULL OR $this->lifetimeCol + $this->timeCol > ?)";
$result = $this->conn->executeQuery($sql, [
$id,
$this->encodeIds($id),
time(),
], [
ParameterType::STRING,
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function doDelete(array $ids): bool
{
$sql = "DELETE FROM $this->table WHERE $this->idCol IN (?)";
try {
$this->conn->executeStatement($sql, [array_values($ids)], [Connection::PARAM_STR_ARRAY]);
$this->conn->executeStatement($sql, [$this->encodeIds(array_values($ids))], [Connection::PARAM_STR_ARRAY]);
} catch (TableNotFoundException $e) {
}

Expand Down Expand Up @@ -314,6 +314,8 @@ protected function doSave(array $values, int $lifetime)
}

foreach ($values as $id => $data) {
$id = $this->encodeIds($id);

try {
$rowCount = $stmt->executeStatement();
} catch (TableNotFoundException $e) {
Expand Down Expand Up @@ -382,16 +384,21 @@ private function getServerVersion(): string

private function addTableToSchema(Schema $schema): void
{
$types = [
'mysql' => 'binary',
'sqlite' => 'text',
];

$table = $schema->createTable($this->table);
$table->addColumn($this->idCol, $types[$this->getPlatformName()] ?? 'string', ['length' => 255]);
$table->addColumn($this->idCol, 'string', ['length' => 255]);
$table->addColumn($this->dataCol, 'blob', ['length' => 16777215]);
$table->addColumn($this->lifetimeCol, 'integer', ['unsigned' => true, 'notnull' => false]);
$table->addColumn($this->timeCol, 'integer', ['unsigned' => true]);
$table->setPrimaryKey([$this->idCol]);
}

private function encodeIds($ids)
{
return str_replace("\0tags\0", '0tags0', $ids);
}

private function decodeIds($ids)
{
return str_replace('0tags0', "\0tags\0", $ids);
}
}
19 changes: 16 additions & 3 deletions src/Symfony/Component/Cache/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ protected function doFetch(array $ids)
$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 = $connection->prepare($sql);
$stmt->bindValue($i = 1, $now, \PDO::PARAM_INT);
$ids = $this->encodeIds($ids);
foreach ($ids as $id) {
$stmt->bindValue(++$i, $id);
}
Expand All @@ -385,7 +386,7 @@ protected function doFetch(array $ids)
if (null === $row[1]) {
$expired[] = $row[0];
} else {
yield $row[0] => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
yield $this->decodeIds($row[0]) => $this->marshaller->unmarshall(\is_resource($row[1]) ? stream_get_contents($row[1]) : $row[1]);
}
}

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', $this->encodeIds($id));
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
$stmt->execute();

Expand Down Expand Up @@ -452,7 +453,7 @@ 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));
$stmt->execute($this->encodeIds(array_values($ids)));
} catch (\PDOException $e) {
}

Expand Down Expand Up @@ -539,6 +540,8 @@ protected function doSave(array $values, int $lifetime)
}

foreach ($values as $id => $data) {
$id = $this->encodeIds($id);

try {
$stmt->execute();
} catch (\PDOException $e) {
Expand Down Expand Up @@ -580,4 +583,14 @@ private function getServerVersion(): string

return $this->serverVersion;
}

private function encodeIds($ids)
{
return str_replace("\0tags\0", '0tags0', $ids);
}

private function decodeIds($ids)
{
return str_replace('0tags0', "\0tags\0", $ids);
}
}
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 <[email protected]>
*
* 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,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\PdoAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
use Symfony\Component\Cache\Tests\Adapter\TagAwareAdapterTestCase;

/**
* @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);
}
}
Loading