3

Is there a standard way of making sure consecutive loading of models get the object from cache rather than DB? For example

$product = Mage::getModel('catalog/product')->load(3770);

This will first look at cache storage, if found return that, if not proceed with the loading, saving whats's loaded in to cache and return. This should also be combined with cache remove when ->save() method is called.

2

I remember there is a module for this, but forgot the name. Anyway, you have to be carefully what you put into such a cache, and how totally you make use of it, as you may end to use cached models, where it harms you.

My personal approach for solving this is a service model I create via Mage::getSingleton() and which I use then to fetch new models (of a specific type) to make use of them in different places. The advantage of this approach is, if you need a big number of different Models of the same type, you can easy implement a pre-fetching method without changing the way you fetch the single models.

0

Do you mean inside the same request, or different requests? If you're talking about multiple load calls within the same request, you're supposed to use Mage::registry('current_product') or Mage::registry('product') if you're on a product page to avoid loading the product again. Of course for other controller/actions you can just use the registry to store entities.

If you mean caching between multiple requests, as far as I know there is no standard way to do that. IMO it's also unnecessary. One or two loads should still be very scalable if everything is set-up right (FPC/block-cachin/MySQL tuning, etc.)

If you find yourself loading many products for some reason, consider adding the necessary attributes to the flat table for faster access.

Hope that helped.

  • I am more after multiple requests. Thanks for your reply – xelber May 22 '15 at 4:15
  • No worries. I still don't think there's enough context to your question though. Why do you want to cache the load call? What cache storage are you using? Probably the query is already being cached by MySQL! Check MySQLs query_cache_size and query_cache_type. – Erfan May 22 '15 at 7:01
  • Basically when a product is loaded from DB, I would like it to be cached until its changed in DB again. If a product is loaded by some module, there is no need to load it again. We can cover some of this using block cache I agree, but there are still multiple product loads which can be avoided. For example say in two sessions, same product is added to the cart, I'd rather only load the product from DB once. – xelber May 24 '15 at 23:01
  • did you already profile, how long this query really needs? my experience the query cache of mysql is so good, such a query needs less then 5ms – Flyingmana May 26 '15 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.