Entity caching based on the Drupal 7 contrib module has been added to core as an alternative to field caching and to fully cache entities that contain non-field data.

Entity caching is enabled for the core entity types: nodes, terms, and users. It is disabled by default for new entity types, such as those provided by contrib or custom modules.

How to disable entity cache

If you are using a module that adds or modifies an entity's properties after the node has been cached, you may want to disable entity caching for that entity type.

In a custom module, you can implement hook_entity_info_alter() to disable the entity cache.

function hook_entity_info_alter(&$entity_info) {
  $entity_info['node']['entity cache'] = FALSE;
  $entity_info['node']['field cache'] = TRUE;

How to enable entity cache

In order to use entity caching on custom entity types, you need to create a cache table, and enable the new "entity cache" key in hook_entity_info().

In your .install file, add the new schema in hook_schema() and hook_update_N().

$cache_schema = backdrop_get_schema_unprocessed('system', 'cache');
$schema['cache_entity_node'] = $cache_schema;
$schema['cache_entity_node']['description'] = "Cache table used to store Node entity records.";

In your .module file, add a new section to the end of hook_entity_info() to set the entity cache entry for each entity type. It is also recommended that you set field cache to FALSE in order to reduce duplication of data in cache tables.

// If the cache table has been created, then enable entity caching on nodes.
if (db_table_exists('cache_entity_node')) {
  $entity_info['node']['entity cache'] = TRUE;
  $entity_info['node']['field cache'] = FALSE;

Some entity types provide their own storage controllers. If the DefaultEntityController::load() method is overridden, be sure to call parent::load() to allow the caching system to pull from the entity cache storage.

Take this example from CommentStorageController:

   * Overrides DefaultEntityController::load().
  public function load($ids = array(), $conditions = array()) {
    $comments = parent::load($ids, $conditions);

    foreach ($comments as $key => $comment) {
      $comment->new = node_mark($comment->nid, $comment->changed);
    return $comments;

The call to parent::load($ids, $conditions) is what pulls the entities from the specified cache table. Additional (non-cached) modifications can be made to the entity after it has been loaded with this approach.

Introduced in branch: 
Introduced in version: 
Module developers