簡體   English   中英

Symfony 緩存 HTTP 已經緩存控制:無緩存私有

[英]Symfony cache HTTP already cache-controle: no-cache private

我用 Symfony 4.4(FOSRestBundle,Postman)創建了一個 API,我想做一個緩存系統,但緩存控制已經沒有緩存,私有。 我在我的 controller 中嘗試了這個緩存系統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 下的先前項目中,具有相同的 function 的緩存有效。 這可能來自 FosRestBundle 還是來自配置?

我見過其他緩存系統,但我只想緩存這個 $ list 變量,所以這個在我看來是最好的。

我也在 index.php 中嘗試了這個

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

但不工作我只有一個私人緩存控制

我嘗試將其放入 doctrine.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:
        # ...

在緩存中。yaml:

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

在我的服務中。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 項目上不起作用,謝謝。

我用 Symfony 4.4(FOSRestBundle,Postman)創建了一個 API,我想做一個緩存系統,但緩存控制已經沒有緩存,私有。 我在我的 controller 中嘗試了這個緩存系統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 下的先前項目中,具有相同的 function 的緩存有效。 這可能來自 FosRestBundle 還是來自配置?

我見過其他緩存系統,但我只想緩存這個 $ list 變量,所以這個在我看來是最好的。

我也在 index.php 中嘗試了這個

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

但不工作我只有一個私人緩存控制

我嘗試將其放入 doctrine.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:
        # ...

在緩存中。yaml:

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

在我的服務中。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 項目上不起作用,謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM