After adding data to nodes using hook_node_load(), will this data be saved in the cache?
No, it will be not. hook_load() and hook_node_load() are invoked by NodeController::attachLoad(), which contains the following code.
// Call object type specific callbacks on each typed array of nodes.
foreach ($typed_nodes as $node_type => $nodes_of_type) {
if (node_hook($node_type, 'load')) {
$function = node_type_get_base($node_type) . '_load';
$function($nodes_of_type);
}
}
// Besides the list of nodes, pass one additional argument to
// hook_node_load(), containing a list of node types that were loaded.
$argument = array_keys($typed_nodes);
$this->hookLoadArguments = array($argument);
parent::attachLoad($nodes, $revision_id);
NodeController::attachLoad() is called from DrupalDefaultEntityController::load() (DrupalDefaultEntityController is the base class of NodeController) after the method loaded the entities from the cache.
Don't get confused from the following code.
if ($this->cache) {
// Add entities to the cache if we are not loading a revision.
if (!empty($queried_entities) && !$revision_id) {
$this->cacheSet($queried_entities);
}
}
What DrupalDefaultEntityController::cacheSet() does is setting DrupalDefaultEntityController::$entityCache; it doesn't use any database cache.
What you can do is using the cache function in your implementation of hook_node_load(), possibly using a static variable to save the content of the cache once retrieved. You should use drupal_static() only if the value would need to be reset (or changed) from an external function. For example, your implementation of hook_node_delete() could remove the reference to the nodes being deleted from that static variable.
If the value needs to be cached per node, I would use code similar to the following one.
function mymodule_node_load($nodes, $types) {
global $language_content;
$data = &drupal_static(__FUNCTION__, array());
$langcode = $language_content->language;
if (empty($data) && ($cache = cache_get("mymodule_cache:$langcode"))) {
$data[$langcode] = $cache->data;
}
foreach ($nodes as $nid => $node) {
if (isset($data[$langcode][$nid]) {
$node->field = $data[$langcode][$nid];
}
else {
// Get the field value.
// Save the value in $node->field.
$update_cache = TRUE;
}
}
if (!empty($update_cache)) {
cache_set('mymodule_cache', $data, 'cache');
}
}
The code I shown considers the language currently set; if the value of the field doesn't depend from the language, you can replace "mymodule_cache:$langcode" with "mymodule_cache".