Configuring schema cache

December 5, 2025 ยท View on GitHub

The information about the database schema that's needed for ORM comes from Schema that retrieves it from the database server.

For faster access, Schema stores database schema information in SchemaCache.

When the Schema needs retrieve information about the database schema, it first checks the cache.

You can configure SchemaCache to use PSR-16 cache implementation in two ways:

Examples below use yiisoft/cache. Make sure you have installed it via Composer using composer require yiisoft/cache.

Autowired PSR-16 cache

This configuration is suitable if you want to use the same cache driver for the whole application.

Create a file config/common/di/cache.php for cache:

use Psr\SimpleCache\CacheInterface;
use Yiisoft\Cache\File\FileCache;

/** @var array $params */

return [
    CacheInterface::class => [
        'class' => FileCache::class,
        '__construct()' => [
            'cachePath' => __DIR__ . '/../../runtime/cache',
        ],
    ],
];

The SchemaCache requires CacheInterface and DI container will automatically resolve it.

Manual cache configuration

This configuration is suitable if you want to use a different cache driver for caching schema.

Create a file config/common/di/db-schema-cache.php for cache:

use Yiisoft\Cache\File\FileCache;
use Yiisoft\Db\Cache\SchemaCache;

return [
    SchemaCache::class => [
        'class' => SchemaCache::class,
        '__construct()' => [
            new FileCache(__DIR__ . '/../../runtime/cache'),
        ],
    ],
];

Disabling schema cache

You can disable schema caching by setting the enabled parameter to false in your application's config/params.php:

return [
    // ...
    'yiisoft/db' => [
        'schema-cache' => [
            'enabled' => false,
        ],
    ],
];

This parameter is automatically wired to the SchemaCache service via the package's DI configuration.