worked on CRM-12321, added memcache support for civicrm_cache table
authorKurund Jalmi <kurund@civicrm.org>
Tue, 18 Jun 2013 11:06:15 +0000 (16:36 +0530)
committerTim Otten <totten@civicrm.org>
Mon, 24 Jun 2013 10:29:36 +0000 (06:29 -0400)
CRM/Core/BAO/Cache.php

index 298b9cbec867630ae2ad60b5eddb3db1f9a1a705..9ebb4ca98f5151136d2626cc50434c18926623cd 100644 (file)
@@ -58,18 +58,32 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * @access public
    */
   static function &getItem($group, $path, $componentID = NULL) {
-    $dao = new CRM_Core_DAO_Cache();
+    static $_cache = NULL;
+    if ($_cache === NULL) {
+      $_cache = array();
+    }
 
-    $dao->group_name   = $group;
-    $dao->path         = $path;
-    $dao->component_id = $componentID;
+    $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
+    if (!array_key_exists($argString, $_cache)) {
+      $cache = CRM_Utils_Cache::singleton();
+      $_cache[$argString] = $cache->get($argString);
+      if (!$_cache[$argString]) {
+        $dao = new CRM_Core_DAO_Cache();
+
+        $dao->group_name   = $group;
+        $dao->path         = $path;
+        $dao->component_id = $componentID;
 
-    $data = NULL;
-    if ($dao->find(TRUE)) {
-      $data = unserialize($dao->data);
+        $data = NULL;
+        if ($dao->find(TRUE)) {
+          $data = unserialize($dao->data);
+        }
+        $dao->free();
+        $_cache[$argString] = $data;
+        $cache->set($argString, $_cache[$argString]);
+      }
     }
-    $dao->free();
-    return $data;
+    return $_cache[$argString];
   }
 
   /**
@@ -83,18 +97,34 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * @access public
    */
   static function &getItems($group, $componentID = NULL) {
-    $dao = new CRM_Core_DAO_Cache();
+    static $_cache = NULL;
+    if ($_cache === NULL) {
+      $_cache = array();
+    }
 
-    $dao->group_name   = $group;
-    $dao->component_id = $componentID;
-    $dao->find();
+    $argString = "CRM_CT_CI_{$group}_{$componentID}";
+    if (!array_key_exists($argString, $_cache)) {
+      $cache = CRM_Utils_Cache::singleton();
+      $_cache[$argString] = $cache->get($argString);
+      if (!$_cache[$argString]) {
+        $dao = new CRM_Core_DAO_Cache();
+
+        $dao->group_name   = $group;
+        $dao->component_id = $componentID;
+        $dao->find();
 
-    $result = array(); // array($path => $data)
-    while ($dao->fetch()) {
-      $result[$dao->path] = unserialize($dao->data);
+        $result = array(); // array($path => $data)
+        while ($dao->fetch()) {
+          $result[$dao->path] = unserialize($dao->data);
+        }
+        $dao->free();
+
+        $_cache[$argString] = $result;
+        $cache->set($argString, $_cache[$argString]);
+      }
     }
-    $dao->free();
-    return $result;
+
+    return $_cache[$argString];
   }
 
   /**
@@ -110,6 +140,11 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * @access public
    */
   static function setItem(&$data, $group, $path, $componentID = NULL) {
+    static $_cache = NULL;
+    if ($_cache === NULL) {
+      $_cache = array();
+    }
+
     $dao = new CRM_Core_DAO_Cache();
 
     $dao->group_name   = $group;
@@ -133,6 +168,12 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     $lock->release();
 
     $dao->free();
+
+    // set the cache in memory
+    $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
+    $cache = CRM_Utils_Cache::singleton();
+    $data = unserialize($dao->data);
+    $cache->set($argString, $data);
   }
 
   /**