You are here
Drupal Node API & Node Load
Node Load3>
node_load
Using the node_load function is really cool because it gives you access to all the data associated with a node, but excessive use of this function can be costly. There are several things we must know about what the node_load does.
First, all calls to this function will invoke all the hook_nodeapi functions defined by all modules that have been loaded at that point. The way node_load does this is by invoking the function node_invoke_nodeapi.
function node_load() {
//... if ($extra = node_invoke_nodeapi($node, 'load')) {
foreach ($extra as $key => $value) {
$node->$key = $value;
}
}
//...
}
Second, node_load uses a PHP static variable to store the data associated to an nid or an array of conditions. This static variable works as a caching mechanism which hold the node information in memory while the script still running. While this is good for low number of node_load calls, since it reduces the number of calls to the database, large number of of node_load calls would mean more memory being used. On the good side, this node_load has a reset caching option which will clear the memory being used by the static variable. Here's a code snippet of how this fuction uses a static variable to cache node data.
function node_load($param = array(), $revision = NULL, $reset = NULL) {
static $nodes = array(); if ($reset) {
$nodes = array();
}
//...
}
Third, Drupal invokes node_load on pages such the frontpage, and taxonomy pages; which fetches all the data associated to those nodes displayed on these pages.
So remember, high node_load calls with caching set to TRUE = more memory used & less database queries executed, and high node_load calls with caching set to FALSE = Less memory used & more database queries executed.
Node API
hook_nodeapi
One of the biggest problems I have encounter while programming in Drupal, is that many times is easy to miss crucial information about the best programming practices and about what the core system has to offer. The NodeAPI is one of those Drupal hooks that has a lot to offer. The hook allows to load additional information associated with a node, add additional elements to the node's content before it is render, and perform additional actions on different operations including while updating, deleting and inserting.
As mentioned above, this hook allows retrieval of additional data associated with a node before the node is fetched from the database. This capability minimizes the need of fetching data from the database multiple times. In addition, since the NodeAPI passes the node by reference, it also eliminates duplication of data; which in fact means less unnecessary use of memory.
lifestyle:
- Diego F. Roldán's blog
- Log in to post comments
- 7979 reads