英文:
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_WRITE
和READ_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: '%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
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.
<?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
{
In the controller I retrieve the array of items from the LogRepository (the default one, extending from ServiceEntityRepository):
$logs = $logRepository->findBy(
$criteria,
['id' => 'DESC'],
$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->entityManager->getCache();
$cache->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: '%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
and for the entity
#[ORM\Cache(usage: "NONSTRICT_READ_WRITE", region: "append_only")]
with NONSTRICT_READ_WRITE
instead of READ_ONLY
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论