symfony 2条令查询生成器Apc缓存未保存集合对象

symfony 2条令查询生成器Apc缓存未保存集合对象,symfony,caching,collections,doctrine,apc,Symfony,Caching,Collections,Doctrine,Apc,我有一个存储库函数,可以根据类别检索随机产品。我使用APC缓存来缓存结果和随机产品,仅当缓存过期时。除了产品的图像未保存在缓存中外,其他一切正常。 Product实体与ProductImages实体之间的关系如下: /** * @var arrayCollection * * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval

我有一个存储库函数,可以根据类别检索随机产品。我使用APC缓存来缓存结果和随机产品,仅当缓存过期时。除了产品的图像未保存在缓存中外,其他一切正常。 Product实体与ProductImages实体之间的关系如下:

/**
 * @var arrayCollection
 *
 * @ORM\OneToMany(targetEntity="ProductImage", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
 */
private $images;

public function __construct()
{
    $this->images = new ArrayCollection();
}

/**
 * @var Product
 *
 * @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
 */
private $product;
我的存储库功能:

/**
 * @param $category
 * @param $limit
 * @param $source
 * @param $cacheExpiresTime
 * @return \Doctrine\ORM\Query
 */
public function findByCategoryLimit($category, $limit, $source, $cacheExpiresTime = 3600)
{
    $cacheDriver = new ApcCache();
    $cacheId = 'category_'.$category->getId().'_products';
    $config = $this->getEntityManager()->getConfiguration();
    $config->setResultCacheImpl($cacheDriver);
    if ($config->getResultCacheImpl()->contains($cacheId))
    {
        return $config->getResultCacheImpl()->fetch($cacheId);
    }
    $qb = $this->createQueryBuilder('p');
    $qb->select('p')
        ->leftJoin('p.sourceCategory', 's')
        ->where('s.category = :category')
        ->setMaxResults($limit)
        ->setParameter('category', $category);
    if (!empty($source)){
        $qb->andWhere('p.source = :source');
        $qb->setParameter('source', $source);
    }
    $qb->orderBy('p.title', 'ASC');
    if($this->getCategoryTotalProducts($category) > $limit) {
        $qb->setFirstResult(rand(1, ($this->getCategoryTotalProducts($category) - $limit)));
    }
    $query = $qb->getQuery();
    //$query->setResultCacheDriver($cacheDriver);
    //$query->useResultCache(true, $cacheExpiresTime, $cacheId);
    $result = $query->getResult();
    $config->getResultCacheImpl()->save($cacheId, $result, $cacheExpiresTime);

    return $result;

}
当产品从db加载时,图像看起来很好,但从缓存加载时,图像变为空。我做错了什么?我尝试了不同的方法并寻找了一个解决方案,但没有成功。 $CacheExpireTime设置为20


提前感谢。

您是否尝试加载图像?追加联接并选择查询。我知道这是一个解决方法:

问题在于查询偏移量。 这是解决办法。现在,它的工作如预期

/**
 * @param $category
 * @param $limit
 * @param $source
 * @param $cacheExpiresTime
 * @return \Doctrine\ORM\Query
 */
public function findByCategoryLimit($category, $limit, $source, $cacheExpiresTime = 3600)
{
    $cacheDriver = new ApcCache();
    $cacheId = 'category_'.$category->getId().'_products';
    $config = $this->getEntityManager()->getConfiguration();
    $config->setResultCacheImpl($cacheDriver);
    $offset = 0;
    if($this->getCategoryTotalProducts($category) > $limit) {
        $offset = rand(1, ($this->getCategoryTotalProducts($category) - $limit));
    }

    if ($config->getResultCacheImpl()->contains($cacheId))
    {
        $offset = $config->getResultCacheImpl()->fetch($cacheId);
    }

    $qb = $this->createQueryBuilder('p');
    $qb->select('p')
        ->leftJoin('p.sourceCategory', 's')
        ->where('s.category = :category')
        ->setMaxResults($limit)
        ->setParameter('category', $category);
    if (!empty($source)){
        $qb->andWhere('p.source = :source');
        $qb->setParameter('source', $source);
    }
    $qb->orderBy('p.title', 'ASC');
    $qb->setFirstResult($offset);

    $query = $qb->getQuery();
    $query->useQueryCache(true);
    $query->useResultCache(true);
    $result = $query->getResult();
    if (!$config->getResultCacheImpl()->contains($cacheId)) {
        $config->getResultCacheImpl()->save($cacheId, $offset, $cacheExpiresTime);
    }

    return $result;

}

我设法解决了它。这是我在尝试获取随机结果时犯的一个错误,查询总是不同的,因此没有缓存。。。