dotkernel / dot-cache

扩展 symfony-cache 的 DotKernel 缓存组件

安装次数:7,175

依赖项: 2

建议者: 0

安全性: 0

星标: 2

关注者: 3

分支: 1

开放问题: 1

4.1.0 2024-05-03 17:34 UTC

README

重要

dot-cache 是 symfony/cache 的包装器

OSS Lifecycle

dot-cache 徽章

OSS Lifecycle PHP from Packagist (specify version)

GitHub issues GitHub forks GitHub stars GitHub license

Build Static codecov

SymfonyInsight

注意

此包仅支持 arrayfilesystem 适配器,您可以同时使用多个适配器。

安装

在您的项目目录中运行以下命令

composer require dotkernel/dot-cache

安装后,将 Dot\Cache\ConfigProvider::class 类添加到您的配置聚合中。

Doctrine in_array 配置

config\autoload\doctrine.global.php 中,您需要添加以下配置

doctrine.configuration.orm_default 键下添加以下配置

'result_cache'       => 'array',
'metadata_cache'     => 'array',
'query_cache'        => 'array',
'hydration_cache'    => 'array',
'second_level_cache' => [
    'enabled'                    => true,
    'default_lifetime'           => 3600,
    'default_lock_lifetime'      => 60,
    'file_lock_region_directory' => '',
    'regions'                    => [],
],

接下来,在 doctrine 键下添加以下配置

'cache' => [
    'array' => [
        'class'     => \Dot\Cache\Adapter\ArrayAdapter::class,
    ],
],

注意

上述配置将使用内存缓存,因为您使用了 array 适配器。

如果您想将缓存存储在本地磁盘上的文件中,您需要使用 filesystem 适配器。

使用文件系统配置 Doctrine 缓存

filesystem 适配器需要一些额外的配置

  • 目录(文件夹路径)

  • 命名空间(目录名称)

    'cache' => [ 'array' => [ 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, ], 'filesystem' => [ 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, 'directory' => getcwd() . '/data/cache', 'namespace' => 'doctrine', ], ],

您可以使用 filesystem 适配器将 result_cachemetadata_cachequery_cachehydration_cache 存储到文件中,或者您可以使用 array 适配器将 result_cache 存储到内存中。

内存和文件系统适配器的配置示例

config\autoload\doctrine.global.php 文件的配置示例

return [
    'dependencies'        => [
        'factories' => [
            \Dot\Cache\Adapter\FilesystemAdapter::class => \Dot\Cache\Factory\FilesystemAdapterFactory::class,
        'aliases'   => [
            \Symfony\Component\Cache\Adapter\FilesystemAdapter::class => \Dot\Cache\Adapter\FilesystemAdapter::class
        ],
    ],
    'doctrine'            => [
        'configuration' => [
            'orm_default' => [
                'result_cache'       => 'array',
                'metadata_cache'     => 'array',
                'query_cache'        => 'filesystem',
                'hydration_cache'    => 'array',
                'second_level_cache' => [
                    'enabled'                    => true,
                    'default_lifetime'           => 3600,
                    'default_lock_lifetime'      => 60,
                    'file_lock_region_directory' => '',
                    'regions'                    => [],
                ],
            ],
        ],
        'cache'      => [
            'array' => [
                'class'     => \Symfony\Component\Cache\Adapter\ArrayAdapter::class,
            ],
            'filesystem' => [
                'class'     => \Dot\Cache\Adapter\FilesystemAdapter::class,
                'directory' => getcwd() . '/data/cache',
                'namespace' => 'doctrine',
            ],
        ],
    ],
];

注意

上述配置只是一个示例,不应直接使用。

您可以使用 doctrine.configuration.orm_default.second_level_cache.enabled 键启用/禁用缓存系统。