Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to load a bunch of node-objects and apply some algorithm to them.
Usually I'd use an iteration and node_load() within, but I was wondering if there's a better (speaking of performance) way to load node objects.

I think that being able to cache the algorithm's result on a per-page basis would be sufficient.

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

node_load() is the preferable way to load a node object, especially if the node object is passed to some functions.
Take in mind that, in Drupal 6 the result of node_load() is locally cached in a static variable by default; if you are going to load a bunch of nodes, it is better if you call the function as node_load($id, NULL, TRUE). If you are sure that, for some reasons, the code could load the same node in consecutive calls to node_load(), then you can consider resetting the static value after X calls to node_load().
The result of node_load() is not cached, if the $revision parameter passed to the function is not NULL.

Caching the value obtained from node_load() is possible, and your code could use its own cache for that purpose. The cache should be invalidated when nodes are updated, which includes also the case when a node is reverted to a previous revision. Rather than implementing a cache in your own module, it would be better to use a module that caches the nodes for third-party modules.

In Drupal 7, both node_load() and node_load_multiple() accept a $reset parameter that resets the node cache kept as instance variable from the object of class NodeController that is created from Drupal, which uses the instance variable implemented by DrupalDefaultEntityController, the class that implements the default methods necessary for an entity controller.

share|improve this answer
    
Note that you would have to clear the cache every time a node is updated, not just when a new revision is created. The node itself changes, doesn't matter if there is a revision of the old version created :) And I would suggest doing caching like that only if you are re-loading the same nodes multiple times over a relatively short time span. Because, as opposed to the entity cache module linked by me, the cache will the only be used by you and won't affect "normal" node loads. –  Berdir Jun 1 '11 at 7:41
    
@Berdir Entity cache is a module for Drupal 7 and that will never be back-ported to Drupal 6. Indeed, the cache should be invalidated when the node is changed; I was trying to point out that if there is a module that changes the revision of the node, the cache should be invalidated. –  kiamlaluno Jun 1 '11 at 7:52
    
@kiamluno: I know that entity cache is Drupal 7 only, that doesn't change the fact that caching only makes sense if you know hat you will re-load these nodes again (possibly multiple times) before the cache is cleared. If you don't, then the cache save is just unecessary overhead. –  Berdir Jun 1 '11 at 7:56
add comment

There is no other way that I know of in Drupal 6.

Drupal 7 has node_load_multiple(), which allows to load multiple nodes in a single query (+ hooks, which can also do single queries for multiple nodes). And additinally, there is the Entity cache project, which allows to cache any loaded entity.

share|improve this answer
    
The link for Entity cache seems wrong: I get a "page not found" error when I click on it. –  kiamlaluno Jun 1 '11 at 6:37
    
@kiamlaluno: Fixed. –  Berdir Jun 1 '11 at 7:19
    
Thank you. I was interested to the module. :-) –  kiamlaluno Jun 1 '11 at 7:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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