Object cache is a simple module using Drupal's cache API to store and retrieve objects (nodes, comments, users etc) to speed up rendering of pages, to lower the number of requests to the database and so on which benefits both anonymous as authenticated users. Since the Drupal cache API is used, these objects can also live in memcache or any other storage mechanism you can think of.

API

The api uses a single function to get an object from cache and set it at the same time if found. Nodes, comments and users are invalidated through hook_nodeapi, hook_comment and hook_user hooks. It's important that by default - in most cases - nothing will happen. Either you use the API functions in your own modules or you are using a views row plugin included in the package.

Examples

<?php
/**
* Custom menu callback which will show a list of nodes.
*/
function foo() {
 
$output = '';
 
$result = db_query("SELECT nid FROM {node} WHERE type = 'news'");
  while (
$row = db_fetch_object($result)) {
   
$output .= object_cache_get('node_view', $row->nid);
  }
  return
$output;
}
/**
* Implements object_cache_TYPE_load().
*/
function object_cache_node_view_load($id) {
 
$node = object_cache_get('node', $id);
  return
node_view($node, FALSE, TRUE, FALSE);
}
?>

Getting a higher cache hit rate

You can tell Object cache to cache nodes and users when they are loaded using their '_load' function (node_load in case of nodes, user_load in case of users). The most obvious case this happens is when viewing the node page or the user page.
To do this, set the corresponding variable by setting global $conf in settings.php or using variable_set() yourself:

<?php
// Cache nodes of type 'news' when viewing their node page,
// use an empty array to disable caching (which is the default)
$conf['object_cache_hook_exit_node'] = array('news');
// Cache users when viewing their profile page
$conf['object_cache_hook_exit_node'] = TRUE;
?>

Modules using the API

Views row plugins

The module comes with two 'Object cache' row plugins for Views, one for nodes and one for comments which drastically decreases the number of queries on views pages where the node (for any build mode, full, teaser etc) or comment is loaded. In combination with Views content cache, this even makes you and your database sleep better at night.

Drupal 7

A Drupal 7 port will never happen, use Entity cache.

Project Information

Downloads