Invalidation of ORM cache doctrine in rabbitmq-consumer | Symfony2
I have rabbitmq consumer implemented in symfony2 application with oldsound / rabbitmq-bundle The application uses 10 messages at the same time and collects data from mysql-db using doctrine-querybuilder with filters specified in the message data. Cachingsystem is APC. Consider this stripped-down request code
$qb = $this->entityManager
->getRepository('My\Entity')
->createQueryBuilder();
$qb
->select('sum(this) as this, sum(that) as that')
->andWhere('field in (:values)')
->setParameter('values', $filterValues);
$result = $qb
->getQuery()
->setSingleResult();
This query returns 10 times the same data for different filter values. When the consumer reloads and consumes the next 10 messages, I get a different result again 10 times. I think the doctrine cache is responsible for this and tried to make it invalidate:
$qb = $this->entityManager
->getRepository('My\Entity')
->createQueryBuilder();
$qb
->select('sum(this) as this, sum(that) as that')
->andWhere('field in (:values)')
->setParameter('values', $filterValues);
$result = $qb
->getQuery()
->setResultCacheLifetime(0)
->setSingleResult();
But without effect. I can work around this behavior by only consuming one message at a time, but that slows down my application to an unacceptable degree.
Can anyone hint me in the right direction?
Thanks, Jojo
source to share
It boiled down to a bug in my code. For some reason I thought that the consumer is getting re-intervention after every message is consumed. It is not true! In my query services, I have stored the result of some queries in a property class. Then the code was like this:
<?php
if (null === $this->queryResult) {
$this->doQuery();
} else {
return $this->queryResult
}
So after the first message was consumed, doQuery () was never executed again until the consumer was restarted. I had to reset the properties of the class after every run and obviously fixed it for me.
Lesson learned: When you make a consumer consume more than one message in a process, remember that the consumer and all services used within the initialization framework only once and persist their state across the consumed messages.
Regards @Francesco Panina for pointing me in the right direction. And please excuse the late answer :)
source to share