Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a doctrine2 entity to cache to speed up the page load

Let's say I have an entity called Product and this entity is loaded every time user hits the product information page. Usually I'd save the object in Zend_Cache (memcache) for an hour to avoid hitting the db for each request but as far as I understand that's not possible with Doctrine2 entities because of the Proxy objects.

So my question is, how can I avoid loading the same entity from the database for each request?

[EDIT]

I tried using Doctrine Cache like this

    $categoryService = App_Service_Container::getService('\App\Service\Category');
    $cache = $categoryService->getEm()->getConfiguration()->getResultCacheImpl();
    $apple = $cache->fetch('apple');

But I get the following error

Warning: require(App/Entity/Proxy/_CG_/App/Entity/Category.php) [function.require]: failed to open stream: No such file or directory in /opt/vhosts/app/price/library/Doctrine/Common/ClassLoader.php on line 163

This is same for Zend Cache as well as you can't serialize the entity because of the Proxy class

like image 231
Optimus Avatar asked May 29 '12 19:05

Optimus


1 Answers

You've got several options:

  1. Use Doctrine's built-in result caching
  2. Try just sticking entity in memcache via Zend_Cache. When you pull it out, you may need to merge() the Product back into the EM so proxies can be dereferenced. If you fetch-join any associations you need to display the product info, and you're only doing reads, this shoudl work fine.
  3. Don't cache the entity at all. Cache whatever output you generate instead.

EDIT: If you don't care about the hydration overhead, you're using mysql, and your Products and associated tables don't change very often, you might prefer to just rely on the mySQL query cache. It's a fairly blunt object, but useful enough to mention.

like image 187
timdev Avatar answered Oct 04 '22 18:10

timdev