Delete dynamic-generated cache entries on Doctrine

Viewed 28

I am using doctrine's result cache to improve my application performance. I am using dynamic ids because that info comes from the user. I'd like to flush all cache ids related to this entity (PortfolioFunds) but I don't know those dynamic-generated ids:

class PostgresqlFundsRepository 
{
    // ...   
    
    public function save(PortfolioFunds $portfolioFunds): void
    {
        $this->em->persist($portfolioFunds);
        $this->em->flush();
        $this->deleteCacheId($portfolioFunds->portfolioId());
    }

    public function fetchByPortfolioId(PortfolioId $portfolioId): array
    {
        return $this->createQueryBuilder('f')
            ->where('f.portfolioId = :portfolioId')
            ->setParameter('portfolioId', $portfolioId)
            ->getQuery()
            ->useQueryCache(true)
            ->enableResultCache()
            ->setResultCacheId($this->cacheId($portfolioId))
            ->getArrayResult();
    }

    public function findByPortfolioIdAndCurrency(
        PortfolioId $portfolioId,
        Currency $currency
    ): ?PortfolioFunds {

        $query = $this->createQueryBuilder('f')
            ->where('f.portfolioId = :portfolioId')
            ->andWhere('f.money LIKE :money')
            ->setParameter('portfolioId', $portfolioId)
            ->setParameter('money', '%' . $currency)
            ->getQuery()
            ->useQueryCache(true)
            ->enableResultCache()
            ->setResultCacheId($this->cacheId($portfolioId, $currency));

        return $query->getOneOrNullResult();
    }


    private function cacheId(PortfolioId $id, ?Currency $currency = null): string
    {
        return sprintf('portfolio_%s_funds_%s', $id, $currency);
    }

    private function deleteCacheId(PortfolioId $id): void
    {
        $this->em->getConfiguration()->getResultCacheImpl()->delete(
            $this->cacheId($id)
        );

        // HERE I'd like to delete ids that contain currencies as well
    }

}

Is there anything like namespaces or something that I can use?

I am using Symfony 5.3, PHP 7 and Memcached

0 Answers
Related