commit 21c15b658da93e77ec221b7b517a3e6e840c99ce
Author: fago <nuppla@zites.net>
Date:   Thu Oct 27 18:43:59 2011 +0200

     #1111120 patch by jamsilver, fago: support entity cache module.

diff --git a/entity.api.php b/entity.api.php
index 46bd835..b47a6b8 100644
--- a/entity.api.php
+++ b/entity.api.php
@@ -131,6 +131,11 @@
  * - form callback: (optional) Specfiy a callback that returns a fully built
  *   edit form for your entity type. See entity_form().
  *   In case the 'admin ui' is used, no callback needs to be specified.
+ * - entity cache: (optional) Whether entities should be cached using the cache
+ *   system. Requires the entitycache module to be installed and enabled. As
+ *   cached entities are only retrieved by id key, the cache would not apply to
+ *   exportable entities retrieved by name key. If enabled, 'field cache' is
+ *   obsolete and should be disabled. Defaults to FALSE.
  *
  * @see hook_entity_info()
  * @see entity_metadata_hook_entity_info()
diff --git a/includes/entity.controller.inc b/includes/entity.controller.inc
index b766a2f..4bbdd12 100644
--- a/includes/entity.controller.inc
+++ b/includes/entity.controller.inc
@@ -192,6 +192,20 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
       }
     }
 
+    // Support the entitycache module if activated.
+    if (!empty($this->entityInfo['entity cache']) && !$revision_id && $ids && !$conditions) {
+      $cached_entities = EntityCacheControllerHelper::entityCacheGet($this, $ids, $conditions);
+      // If any entities were loaded, remove them from the ids still to load.
+      $ids = array_diff($ids, array_keys($cached_entities));
+      $entities += $cached_entities;
+
+      // Add loaded entities to the static cache if we are not loading a
+      // revision.
+      if ($this->cache && !empty($cached_entities) && !$revision_id) {
+        $this->cacheSet($cached_entities);
+      }
+    }
+
     // Load any remaining entities from the database. This is the case if $ids
     // is set to FALSE (so we load all entities), if there are any ids left to
     // load or if loading a revision.
@@ -231,6 +245,12 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
       $entities += $queried_entities;
     }
 
+    // Entitycache module support: Add entities to the entity cache if we are
+    // not loading a revision.
+    if (!empty($this->entityInfo['entity cache']) && !empty($queried_entities) && !$revision_id) {
+      EntityCacheControllerHelper::entityCacheSet($this, $queried_entities);
+    }
+
     if ($this->cache) {
       // Add entities to the cache if we are not loading a revision.
       if (!empty($queried_entities) && !$revision_id) {
@@ -256,6 +276,10 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
   public function resetCache(array $ids = NULL) {
     $this->cacheComplete = FALSE;
     parent::resetCache($ids);
+    // Support the entitycache module.
+    if (!empty($this->entityInfo['entity cache'])) {
+      EntityCacheControllerHelper::resetEntityCache($this, $ids);
+    }
   }
 
   /**
diff --git a/tests/entity_test.module b/tests/entity_test.module
index 80f6e24..ea99bd7 100644
--- a/tests/entity_test.module
+++ b/tests/entity_test.module
@@ -56,6 +56,13 @@ function entity_test_entity_info() {
       'label' => $type->label,
     );
   }
+
+  // Support entity cache module.
+  if (module_exists('entitycache')) {
+    $return['entity_test']['field cache'] = FALSE;
+    $return['entity_test']['entity cache'] = TRUE;
+  }
+
   return $return;
 }
 
