From 72a495740eb65c25b180b120bbcd6e1ad59ec708 Mon Sep 17 00:00:00 2001
From: James Silver <james.silver@computerminds.co.uk>
Date: Mon, 23 May 2011 12:14:51 +0100
Subject: [PATCH] Issue #1111120: Entitycache support in Entity API

---
 includes/entity.controller.inc |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/includes/entity.controller.inc b/includes/entity.controller.inc
index 752f551..d72d6ec 100755
--- a/includes/entity.controller.inc
+++ b/includes/entity.controller.inc
@@ -141,6 +141,10 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
       $info = entity_get_info($this->entityInfo['bundle of']);
       $this->bundleKey = $info['bundle keys']['bundle'];
     }
+    $this->cacheClass = FALSE;
+    if (!empty($this->entityInfo['entity cache']) && class_exists($this->entityInfo['entity cache'])) {
+      $this->cacheClass = $this->entityInfo['entity cache'];
+    }
   }
 
   /**
@@ -179,6 +183,7 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
    */
   public function load($ids = array(), $conditions = array()) {
     $entities = array();
+    $cached_entities = array();
 
     // For exportable entities, make sure the defaults have been built.
     if (!empty($this->entityInfo['exportable']) && empty($GLOBALS['conf']['entity_defaults_built'][$this->entityType])) {
@@ -211,6 +216,20 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
       }
     }
 
+    // From EntityCacheControllerHelper
+    if (!empty($this->cacheClass) && !$revision_id && $ids && !$conditions) {
+      $entities += $cached_entities = call_user_func(array($this->cacheClass, '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));
+
+      if ($this->cache) {
+        // Add entities to the cache if we are not loading a revision.
+        if (!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.
@@ -250,6 +269,13 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
       $entities += $queried_entities;
     }
 
+    if (!empty($this->cacheClass)) {
+      // Add entities to the entity cache if we are not loading a revision.
+      if (!empty($queried_entities) && !$revision_id) {
+        call_user_func(array($this->cacheClass, '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) {
@@ -343,7 +369,12 @@ class EntityAPIController extends DrupalDefaultEntityController implements Entit
 
   public function resetCache(array $ids = NULL) {
     $this->cacheComplete = FALSE;
-    parent::resetCache($ids);
+    if (!empty($this->cacheClass)) {
+      call_user_func(array($this->cacheClass, 'resetEntityCache'), $this, $ids);
+    }
+    else {
+      parent::resetCache($ids);
+    }
   }
 
   /**
-- 
1.7.1

