Doctrine的二级缓存不显示新项目。

huangapple go评论70阅读模式
英文:

Doctrine's Second Level Cache doesn't show new items

问题

我正在使用Symfony 6.2.12与Doctrine 2.15.3,并尝试实现二级缓存。分析器栏显示了预期的缓存命中,所以这部分是有效的。问题出现在当我向缓存的实体添加新项(使用EntityManager)时。缓存似乎不知道新项,并且在添加它们之前,findBy()仍然返回旧数组。请注意,我不修改任何现有的实体,列表只应该有更多的项。

我的doctrine.yaml配置:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
        second_level_cache:
            enabled: true
            regions:
                append_only:
                    lifetime: 8640000
                    cache_driver:
                        type: service
                        id: cache.app

我为实体选择了READ_ONLY,因为现有条目永远不会更改。(我也尝试了NONSTRICT_READ_WRITEREAD_WRITE,但没有改善。)当我删除ORM\Cache属性时,新项自然会出现在列表中,因此这绝对是与缓存有关的问题。

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: LogRepository::class)]
#[ORM\Cache(usage: "READ_ONLY", region: "append_only")]
class Log
{

在控制器中,我从LogRepository(默认的LogRepository,扩展自ServiceEntityRepository)检索项目数组:

$logs = $logRepository->findBy(
    $criteria,
    ['id' => 'DESC'],
    $limit,
    $offset
);

这是一个配置问题吗?

当我添加新项时,我当然可以清除整个缓存区域,然后列表就会保持最新:

/** @var \Doctrine\ORM\Cache $cache */
$cache = $this->entityManager->getCache();
$cache->evictEntityRegion(Log::class);

但这不符合我对READ_ONLY缓存的理解,您应该能够向缓存区域中追加项。

英文:

I'm using Symfony 6.2.12 with Doctrine 2.15.3 and try to implement the second level cache. The profiler bar shows the expected cache hits, so that is working. The problem appears, however, when I add new items to the cached entity (using the EntityManager). The cache doesn't seem to be aware of the new items and findBy() still returns the old array before I added them. Note that I don't modify any existing entities, the list should just have more items.

my doctrine.yaml:

doctrine:
    dbal:
        url: &#39;%env(resolve:DATABASE_URL)%&#39;
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: &#39;%kernel.project_dir%/src/Entity&#39;
                prefix: &#39;App\Entity&#39;
                alias: App
        second_level_cache:
            enabled: true
            regions:
                append_only:
                    lifetime: 8640000
                    cache_driver:
                        type: service
                        id: cache.app

I chose READ_ONLY for the entity because existing entries never change. (I tested also with NONSTRICT_READ_WRITE and READ_WRITE, but without improvement.) When I remove that ORM\Cache attribute, the new items naturally appear in the list, so it is definitely an issue with the cache.

&lt;?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: LogRepository::class)]
#[ORM\Cache(usage: &quot;READ_ONLY&quot;, region: &quot;append_only&quot;)]
class Log
{

In the controller I retrieve the array of items from the LogRepository (the default one, extending from ServiceEntityRepository):

$logs = $logRepository-&gt;findBy(
    $criteria,
    [&#39;id&#39; =&gt; &#39;DESC&#39;],
    $limit,
    $offset
);

Is that a configuration problem?

Of course I can evict the entire cache region whenever I add a new item, then the list is up-to-date:

/** @var \Doctrine\ORM\Cache $cache */
$cache = $this-&gt;entityManager-&gt;getCache();
$cache-&gt;evictEntityRegion(Log::class);

But that's not how I understand the READ_ONLY cache where you should be able to append items to the cached region.

答案1

得分: 0

现在已经可以工作,经过一些修改:

`doctrine.yaml`

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
        second_level_cache:
            enabled: true
            
            region_cache_driver:
                type: service
                id: cache.app
            regions:
                append_only:
                    lifetime: 8640000

对于实体部分

#[ORM\Cache(usage: "NONSTRICT_READ_WRITE", region: "append_only")]

使用 `NONSTRICT_READ_WRITE` 而不是 `READ_ONLY`。
英文:

It works now with some modifications:

doctrine.yaml

doctrine:
    dbal:
        url: &#39;%env(resolve:DATABASE_URL)%&#39;
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                dir: &#39;%kernel.project_dir%/src/Entity&#39;
                prefix: &#39;App\Entity&#39;
                alias: App
        second_level_cache:
            enabled: true
            
            region_cache_driver:
                type: service
                id: cache.app
            regions:
                append_only:
                    lifetime: 8640000

and for the entity

#[ORM\Cache(usage: &quot;NONSTRICT_READ_WRITE&quot;, region: &quot;append_only&quot;)]

with NONSTRICT_READ_WRITE instead of READ_ONLY.

huangapple
  • 本文由 发表于 2023年7月13日 21:41:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定