1
votes

I'm trying to use the doctrine cache in order to save the data results from queries.

I've updated the doctrine info in the config file with the following cache config options:

orm:
    ...
    metadata_cache_driver: apc
    query_cache_driver: apc
    result_cache_driver: apc

Here is a query example where I apply the results cache:

    $query = $this->repository->createQueryBuilder('fp')
        ->where('fp.userId = :userId')
        ->setParameter('userId', $userId)
        ->getQuery()
        ->useResultCache(true, 3600, 'favorite');

    return $query->getResult();

It retrieves the data without error but it seems that it doesn't store the result in the APC cache.

I try the following code in order to check if the query result has been cached in APC:

    $q = new \Doctrine\Common\Cache\ApcCache();
    $data = $q->fetch('favorite');

However data always returns the false boolean.

I've checked that APC is enabled in the php.ini files and also it appears enabled in the Symfony profiler.

Anything I might be missing?

1

1 Answers

4
votes

Your configuration seems fine and result is most probably stored in APC cache.

Method useResultCache works in a way that Doctrine performs query and stores result in cache when query is run for the first time. After that (within timeout set) database is not queried for result, it is fetched from cache directly.

I think that a real issue here is the way you verify if result is stored in APC cache or not. I would suggest other (more reliable) ways to check it:

  1. See log file and check queries which are run during request. You should see sql only for first request (when cache is not populated)
  2. Try to change a value in database after first query run. You should see the same result (despite your edit) when query is run for the second time.

Also make sure for which environment (dev, prod) your caching driver (APC) is configured as this might differ between various environments (cache is often disabled in dev environment)

EDIT: I did some further research and now I'm sure that your verification method is wrong. Method fetch you are using looks like:

public function fetch($id)
{
    return $this->doFetch($this->getNamespacedId($id));
}

so fact that you are using favorite key in your useResultCache method doesn't mean that doctrine uses this key internally as it is transformed by getNamespacedId method.

Check fetch method and getNamespacedId method