5

I have a custom field/widget that has a format view function that is highly resource intensive. I currently am caching the result using cache_set()/cache_get() inside the hook_field_formatter_view function for my field.

Is there a better way I could be doing this? I tried to use drupal_render()'s #cache property and it cached the output but still ran my hook_field_formatter_view function.

Basically, I know how to do full page cache (for anonymous users) but how do you cache a field/node/entity (for everyone)?

  • 1
    This is a great question - in Drupal 6 you couldn't really (though Panels allows some granular page caching) but I don't know enough about the new Cache API to comment. Very curious to see an answer though. From a general performance note, have you see AuthCache? drupal.org/project/authcache – Greg Mar 23 '11 at 8:15
  • That authcache module is interesting. It is just full page cache though (and drupal 6 only currently). – Kevin Mar 23 '11 at 11:09
  • What exactly is resource intensive? Is it the loading of additional data that you need to display or the actual rendering? There is drupal.org/project/entitycache, but that only caches the loading of entities, not rendering.. – Berdir Mar 24 '11 at 10:14
  • The code inside my hook_field_formatter_view is resource intensive (not sure what that is considered). entitycache module seems promising. – Kevin Mar 25 '11 at 0:33
  • @Berdir check out my answer. There is a new module called drupal.org/project/render_cache which fills the void. – Gokul N K Oct 22 '13 at 8:01
3

I currently am caching the result using cache_set()/cache_get() inside the hook_field_formatter_view function for my field

Honestly this is not a bad way to go about doing it. When you have custom logic which is resource intensive, taking responsibility for your own caching decisions is usually the right idea. Honestly, I really wish more developers would familiarize themselves with cache_get/set().

Entitycache appears to be an effort to do this generally, and over time I am sure that some best practices will emerge as more people work in this space. However as of today, for anyone looking at a similar problem-set: "Figure out how to cache it yourself" is a good answer.

| improve this answer | |
0

I think you should consider using Render Cache module. This when used in conjunction with Entity Cache it should complement each other well as mentioned on the project page.

| improve this answer | |

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.