Skip to content

Latest commit

 

History

History
64 lines (46 loc) · 2.08 KB

cache.md

File metadata and controls

64 lines (46 loc) · 2.08 KB

Configuring schema cache

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'),
        ],
    ],
];