diff --git a/.roave-backward-compatibility-check.xml b/.roave-backward-compatibility-check.xml index 7e9e361a1..b850af3d7 100644 --- a/.roave-backward-compatibility-check.xml +++ b/.roave-backward-compatibility-check.xml @@ -5,5 +5,6 @@ #\[BC\] SKIPPED: Roave\\BetterReflection\\Reflection\\ReflectionClass "Yiisoft\\Yii\\Debug\\# #\[BC\] SKIPPED: An enum expression Yiisoft\\Db\\QueryBuilder\\Condition\\Like# + #\[BC\] SKIPPED: Unable to compile initializer in class Yiisoft\\Db\\Driver\\Pdo\\AbstractPdoConnection# diff --git a/CHANGELOG.md b/CHANGELOG.md index bdbd6b373..d182acd0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ - New #1152: Add `UuidValue` expression that represents a UUID value independently of DBMS (@KalimeroMK) - Enh #1152: Improve the exception message of `DbUuidHelper::toUuid()` when the value isn't a valid UUID (@KalimeroMK) +- Enh #1201: Make `$psrCache` parameter of `SchemaCache` constructor and `$schemaCache` parameter of + `AbstractSchema` and `AbstractPdoConnection` constructors optional, remove `psr/simple-cache-implementation` from + `require` in `composer.json` (@vjik) ## 2.0.1 February 09, 2026 diff --git a/README.md b/README.md index bd77e452b..7d3e8291e 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,12 @@ To install `yiisoft/db` you must install at least one database driver: - [`yiisoft/db-mssql`](https://github.com/yiisoft/db-mssql); - [`yiisoft/db-oracle`](https://github.com/yiisoft/db-oracle). -> [!IMPORTANT] -> It also depends on [PSR-16: Common Interface for Caching Libraries](https://www.php-fig.org/psr/psr-16/) and requires -> the installation of [PSR-16 implementation](https://packagist.org/providers/psr/simple-cache-implementation). +> [!TIP] +> To cache database schema information, install +> [PSR-16 implementation](https://packagist.org/providers/psr/simple-cache-implementation). > For example, [yiisoft/cache](https://github.com/yiisoft/cache) or one of the other > [cache handlers](https://github.com/yiisoft/cache#cache-handlers). +> It's strongly recommended for production. ## General Usage diff --git a/composer-dependency-analyser.php b/composer-dependency-analyser.php index f3342e8a5..3a02bc13a 100644 --- a/composer-dependency-analyser.php +++ b/composer-dependency-analyser.php @@ -14,7 +14,7 @@ ->ignoreErrorsOnPackages(['yiisoft/yii-debug'], [ErrorType::DEV_DEPENDENCY_IN_PROD]) // Virtual packages that are not directly used in the code. ->ignoreErrorsOnPackages( - ['psr/simple-cache-implementation', 'yiisoft/db-implementation'], + ['yiisoft/db-implementation'], [ErrorType::UNUSED_DEPENDENCY], ); diff --git a/composer.json b/composer.json index 5b1f6e745..733c393e4 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,6 @@ "ext-pdo": "*", "psr/log": "^2.0|^3.0", "psr/simple-cache": "^2.0|^3.0", - "psr/simple-cache-implementation": "*", "yiisoft/db-implementation": "1.0.0" }, "require-dev": { @@ -42,7 +41,7 @@ "ext-mbstring": "*", "friendsofphp/php-cs-fixer": "^3.89.1", "phpunit/phpunit": "^10.5.45", - "rector/rector": "^2.0.10", + "rector/rector": "^2.6.7", "shipmonk/composer-dependency-analyser": "^1.8", "spatie/phpunit-watcher": "^1.24", "yiisoft/aliases": "^3.0", diff --git a/docs/guide/en/README.md b/docs/guide/en/README.md index a2c2b7539..385dcc4d0 100644 --- a/docs/guide/en/README.md +++ b/docs/guide/en/README.md @@ -59,10 +59,11 @@ For [SQLite](https://github.com/yiisoft/db-sqlite): composer require yiisoft/db-sqlite ``` -> [!IMPORTANT] -> It also depends on [PSR-16: Common Interface for Caching Libraries](https://www.php-fig.org/psr/psr-16/) and requires -> the installation of [PSR-16 implementation](https://packagist.org/providers/psr/simple-cache-implementation). +> [!TIP] +> To cache database schema information, install +> [PSR-16 implementation](https://packagist.org/providers/psr/simple-cache-implementation). > For example, [yiisoft/cache](https://github.com/yiisoft/cache) or one of the other [cache handlers](https://github.com/yiisoft/cache#cache-handlers). +> It's strongly recommended for production. ## Configure schema cache diff --git a/docs/guide/en/schema/cache.md b/docs/guide/en/schema/cache.md index 600f2f5be..b94e994a4 100644 --- a/docs/guide/en/schema/cache.md +++ b/docs/guide/en/schema/cache.md @@ -10,6 +10,11 @@ schema information in [SchemaCache](https://github.com/yiisoft/db/blob/master/sr When the [Schema](https://github.com/yiisoft/db/blob/master/src/Schema/AbstractSchema.php) needs retrieve information about the database schema, it first checks the cache. +> [!TIP] +> Configuring schema cache with a real [PSR-16 cache implementation](https://packagist.org/providers/psr/simple-cache-implementation) +> is strongly recommended for production. Without it, schema information is retrieved from the database server on +> every request. + You can configure [SchemaCache](https://github.com/yiisoft/db/blob/master/src/Cache/SchemaCache.php) to use [PSR-16 cache implementation](https://github.com/php-fig/simple-cache) in two ways: @@ -41,7 +46,8 @@ return [ ]; ``` -The `SchemaCache` requires `CacheInterface` and DI container will automatically resolve it. +The `SchemaCache` accepts `CacheInterface` and DI container will automatically resolve it. If `CacheInterface` isn't +configured, `SchemaCache` uses a cache that doesn't store anything, so schema information isn't cached. ## Manual cache configuration diff --git a/src/Cache/NullCache.php b/src/Cache/NullCache.php new file mode 100644 index 000000000..9beb543ce --- /dev/null +++ b/src/Cache/NullCache.php @@ -0,0 +1,62 @@ +psrCache = $psrCache ?? new NullCache(); + } /** * Remove a value with the specified key from cache. diff --git a/src/Driver/Pdo/AbstractPdoConnection.php b/src/Driver/Pdo/AbstractPdoConnection.php index 5c5315653..6e068574c 100644 --- a/src/Driver/Pdo/AbstractPdoConnection.php +++ b/src/Driver/Pdo/AbstractPdoConnection.php @@ -53,7 +53,7 @@ abstract class AbstractPdoConnection extends AbstractConnection implements PdoCo public function __construct( protected PdoDriverInterface $driver, - protected SchemaCache $schemaCache, + protected SchemaCache $schemaCache = new SchemaCache(), protected ?ColumnFactoryInterface $columnFactory = null, ) {} diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 335fcc425..a1583c552 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -55,7 +55,7 @@ abstract class AbstractSchema implements SchemaInterface public function __construct( protected ConnectionInterface $db, - private readonly SchemaCache $schemaCache, + private readonly SchemaCache $schemaCache = new SchemaCache(), ) {} public function getDefaultSchema(): string diff --git a/tests/Db/Cache/NullCacheTest.php b/tests/Db/Cache/NullCacheTest.php new file mode 100644 index 000000000..a7a87174a --- /dev/null +++ b/tests/Db/Cache/NullCacheTest.php @@ -0,0 +1,35 @@ +assertTrue($cache->set('key', 'value')); + $this->assertFalse($cache->has('key')); + $this->assertNull($cache->get('key')); + $this->assertSame('default', $cache->get('key', 'default')); + $this->assertTrue($cache->delete('key')); + $this->assertTrue($cache->clear()); + } + + public function testMultiple(): void + { + $cache = new NullCache(); + + $this->assertTrue($cache->setMultiple(['a' => 1, 'b' => 2])); + $this->assertSame(['a' => 'default', 'b' => 'default'], $cache->getMultiple(['a', 'b'], 'default')); + $this->assertTrue($cache->deleteMultiple(['a', 'b'])); + } +} diff --git a/tests/Db/Cache/SchemaCacheTest.php b/tests/Db/Cache/SchemaCacheTest.php index 8ef49ce91..81c45848a 100644 --- a/tests/Db/Cache/SchemaCacheTest.php +++ b/tests/Db/Cache/SchemaCacheTest.php @@ -87,6 +87,15 @@ public function testWithFailSetCache(): void $schemaCache->set('key', 'test'); } + public function testWithoutPsrCache(): void + { + $schemaCache = new SchemaCache(); + + $schemaCache->set('key', 'value', 'tag'); + + $this->assertNull($schemaCache->get('key')); + } + public function testInvalidCacheKey(): void { $resource = fopen('php://memory', 'r'); diff --git a/tests/Db/Driver/Pdo/PdoConnectionTest.php b/tests/Db/Driver/Pdo/PdoConnectionTest.php index 09b23e59e..7b0476b0e 100644 --- a/tests/Db/Driver/Pdo/PdoConnectionTest.php +++ b/tests/Db/Driver/Pdo/PdoConnectionTest.php @@ -5,12 +5,10 @@ namespace Yiisoft\Db\Tests\Db\Driver\Pdo; use PHPUnit\Framework\TestCase; -use Yiisoft\Db\Cache\SchemaCache; use Yiisoft\Db\Exception\InvalidCallException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Tests\Support\Stub\StubConnection; use Yiisoft\Db\Tests\Support\Stub\StubPdoDriver; -use Yiisoft\Test\Support\SimpleCache\MemorySimpleCache; /** * @group db @@ -50,9 +48,6 @@ private function createConnection(string $dsn = 'sqlite::memory:'): StubConnecti { return new StubConnection( new StubPdoDriver($dsn), - new SchemaCache( - new MemorySimpleCache(), - ), ); } } diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 37856b865..8539165fe 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -4,7 +4,6 @@ namespace Yiisoft\Db\Tests\Db\Schema; -use Yiisoft\Db\Cache\SchemaCache; use Yiisoft\Db\Constraint\Check; use Yiisoft\Db\Constraint\DefaultValue; use Yiisoft\Db\Constraint\ForeignKey; @@ -17,7 +16,6 @@ use Yiisoft\Db\Tests\Support\IntegrationTestCase; use Yiisoft\Db\Tests\Support\Stub\Schema; use Yiisoft\Db\Tests\Support\TestHelper; -use Yiisoft\Test\Support\SimpleCache\MemorySimpleCache; use function count; @@ -208,13 +206,9 @@ public function testGetTableSchemas(): void { $db = $this->getSharedConnection(); - $schemaCache = new SchemaCache( - new MemorySimpleCache(), - ); - $schemaMock = $this->getMockBuilder(Schema::class) ->onlyMethods(['findTableNames', 'loadTableSchema']) - ->setConstructorArgs([$db, $schemaCache]) + ->setConstructorArgs([$db]) ->getMock(); $schemaMock->expects($this->once())->method('findTableNames')->willReturn(['T_constraints_1']); $schemaMock->expects($this->once())->method('loadTableSchema')->willReturn($this->createTableSchemaStub());