2015-05-22 24 views
3

我有一個在symfony2應用程序中實現的rabbitmq-consumer使用oldsound/rabbitmq-bundle 該應用程序每次消費10條消息,並使用doctrine-querybuilder從消息數據中給出的過濾器收集來自mysql-db的數據。 Cachingsystem是APC。 考慮查詢使rabbitmq-consumer中的doctrine ORM緩存失效| Symfony2

$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(); 

此查詢返回10次不同的過濾器值的相同數據的這種精簡碼。當消費者重新啓動並消費接下來的10條消息時,我再次得到10次不同的結果。 我想學說緩存負責本並試圖緩存像這樣無效:

$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(); 

,但沒有效果。 我可以通過每次只消費1條消息來解決此問題,但這會降低我的應用程序到不可接受的程度。

任何人都可以在正確的方向暗示我嗎?

感謝, 喬喬

+1

我們也在使用oldsound,你可以嘗試啓動10個消費者,每個消費者每次只消費1條消息嗎? –

+0

我想你可以在你的查詢中使用這些:setCacheable(bool),useResultCache(bool) –

回答

0

它歸結爲一個錯誤在我的代碼。 我不知何故認爲消費者在消費每個消息之後會得到重新實現。這不是真的!在我的查詢服務中,我將一些querys的結果保存在一個類屬性中。然後,代碼是這樣的:

<?php 
if (null === $this->queryResult) { 
    $this->doQuery(); 
} else { 
    return $this->queryResult 
} 

所以,第一條消息被消耗後,doQuery()從未被再次直到消費者得到了重新執行。我必須在每次運行之後重置類屬性,並且明確地爲我修復它。獲得的經驗教訓:當消費者在一個流程中使用多條消息時,請記住消費者和所使用的所有服務只需初始化一次,並在消費消息中保持其狀態。

關於@Francesco Panina指引我在正確的方向。並請原諒回覆晚:)