Symfony 缓存 HTTP 已经缓存控制:无缓存私有

问题描述

我使用 Symfony 4.4(FOSRestBundle,Postman)创建了一个 API,我想做一个缓存系统,但是缓存控制已经没有缓存,私有了。 我试试这个缓存系统 https://symfony.com/doc/current/components/cache.html 在我的控制器中:

public function readAll(CacheInterface $cache,ProductRepository $productRepository,ParamFetcher $paramFetcher,PaginatorInterface $paginator)
{
    $list = $productRepository->findAll();
    $list = $cache->get('product_',function (ItemInterface $item) use ($list) {
        $item->expiresAfter(3600);
        return $list;
    });
}

有一些配置可以填充缓存,因为我的印象是它无论如何都可以工作,减少了第二次请求的时间以及以下的时间。

奇怪的是,在 symfony 5 下的以前的项目中,缓存的功能相同。这可能来自 FosRestBundle 还是来自配置?

我见过其他缓存系统,但我只想缓存这个 $ list 变量,所以这个在我看来是最好的。

我也在 index.php 中尝试 this

$kernel = new Kernel($_SERVER['APP_ENV'],(bool) $_SERVER['APP_DEBUG']);
if ('prod' === $kernel->getEnvironment()) {
    $kernel = new CacheKernel($kernel);
}

但不工作,我只是在缓存控制上有一个私有

我试着把它像你的链接一样放在 doterin.yaml 中:

doctrine:
dbal:
    url: '%env(resolve:DATABASE_URL)%'

    # IMPORTANT: You MUST configure your server version,# either here or in the DATABASE_URL env var (see .env file)
    #server_version: '13'
orm:
    #auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
        App:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Entity'
            prefix: 'App\Entity'
            alias: App

    # the standard distribution overrides this to be true in debug,false otherwise
    auto_generate_proxy_classes: false
    proxy_namespace: Proxies
    proxy_dir: '%kernel.cache_dir%/doctrine/orm/Proxies'
    default_entity_manager: default
    metadata_cache_driver:
        type: pool
        pool: doctrine.system_cache_pool
    query_cache_driver:
        type: pool
        pool: doctrine.system_cache_pool
    result_cache_driver:
        type: pool
        pool: doctrine.result_cache_pool
    connection: ~
    class_metadata_factory_name:  Doctrine\ORM\Mapping\ClassMetadataFactory
    default_repository_class:  App\Repository\ProductRepository
    hydrators:
        # ...
        # ...
    dql:
        # ...
    filters:
        # ...

在 cache.yaml 中:

framework:
cache:
    pools: #        <==== new
        doctrine.result_cache_pool:
            adapter: cache.app
        doctrine.system_cache_pool:
            adapter: cache.system

在我的 services.yaml 中:

doctrine.result_cache_provider: #        <==== new
    class: Symfony\Component\Cache\DoctrineProvider
    public: false
    arguments:
        - '@doctrine.result_cache_pool'
doctrine.system_cache_provider: #        <==== new
    class: Symfony\Component\Cache\DoctrineProvider
    public: false
    arguments:
        - '@doctrine.system_cache_pool'

我没有更多错误,没关系,但它仍然让我缓存控制:无缓存,私有 它给我带来了一些错误,但我没有设法纠正它 但 Postman 返回 500 异常:服务“doctrine.orm.cache.provider.doctrine.system_cache_pool”依赖于不存在的服务“doctrine.system_cache_pool”。

然后还要在cache.yaml中配置缓存?像这样https://symfony.com/doc/current/cache.html#configuring-cache-with-frameworkbundle ?

如果有人知道这个缓存系统,我正在回答,因为我不明白为什么它在我的 symfony 4.4 项目中不起作用,谢谢。

解决方法

从您放在这里的代码来看,我认为您想为查询添加结果缓存,而不是为控制器添加结果缓存。

在此处查看文档。

https://symfony.com/doc/current/bundles/DoctrineBundle/configuration.html#caching-drivers

这是关于在 symfony2 中使用它们的博文

https://www.gregfreeman.io/2012/invalidating-the-result-cache-in-doctrine-symfony2/

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...