Question

I'm using doctrine 2 without caching anything at the moment. I'd like to enable some caching system within Doctrine but it looks like you have to manage it manually everywhere:

$memcache = new Memcache();
$memcache->connect('memcache_host', 11211);

$cacheDriver = new \Doctrine\Common\Cache\MemcacheCache();
$cacheDriver->setMemcache($memcache);
$cacheDriver->save('cache_id', 'my_data');
...
$cacheDriver->delete('cache_id');

I'd like to know if Doctrine could manage this automatically. For instance:

  1. The cache is enable, I request a User entity by id, Doctrine search in its cache, cannot find the user, fetch it, set it into the cache, return it.

  2. I fetch a second time, Doctrine return me the cached User.

  3. I update the User (or any of its relations) Doctrine detect it and break the cache for this object
  4. I request the same User by id, Doctrine doesn't have it in cache anymore, fetch it and set the cache back with the updated user before to return it

Is that possible?

Cheers, Maxime

Was it helpful?

Solution

What you are looking for (in Doctrine ORM) is only supported in the resultset cache, and only applies to results of SQL queries produced by DQL queries.

The exact name for the feature you are looking for is "second-level cache", which is not yet supported by Doctrine ORM, but is currently being developed (will hopefully be available in version 2.5) at https://github.com/doctrine/doctrine2/pull/580

For now, you will have to handle this kind of caching in your own service layer if it is really needed.

OTHER TIPS

If you are pulling the entity by it's primary key, the caching will be done by the doctrine's "identity map" as described here http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top