Merge pull request #2876 from mepps/participant-image-event-badge
[civicrm-core.git] / CRM / Core / BAO / Cache.php
index 298b9cbec867630ae2ad60b5eddb3db1f9a1a705..e4c986ac2c29407a651ae61278af2289db525e3d 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
  */
 class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
 
+  /**
+   * @var array ($cacheKey => $cacheValue)
+   */
+  static $_cache = NULL;
+
   /**
    * Retrieve an item from the DB cache
    *
@@ -58,18 +63,31 @@ 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();
+    if (self::$_cache === NULL) {
+      self::$_cache = array();
+    }
 
-    $dao->group_name   = $group;
-    $dao->path         = $path;
-    $dao->component_id = $componentID;
+    $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
+    if (!array_key_exists($argString, self::$_cache)) {
+      $cache = CRM_Utils_Cache::singleton();
+      self::$_cache[$argString] = $cache->get($argString);
+      if (!self::$_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();
+        self::$_cache[$argString] = $data;
+        $cache->set($argString, self::$_cache[$argString]);
+      }
     }
-    $dao->free();
-    return $data;
+    return self::$_cache[$argString];
   }
 
   /**
@@ -83,18 +101,33 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * @access public
    */
   static function &getItems($group, $componentID = NULL) {
-    $dao = new CRM_Core_DAO_Cache();
+    if (self::$_cache === NULL) {
+      self::$_cache = array();
+    }
 
-    $dao->group_name   = $group;
-    $dao->component_id = $componentID;
-    $dao->find();
+    $argString = "CRM_CT_CI_{$group}_{$componentID}";
+    if (!array_key_exists($argString, self::$_cache)) {
+      $cache = CRM_Utils_Cache::singleton();
+      self::$_cache[$argString] = $cache->get($argString);
+      if (!self::$_cache[$argString]) {
+        $dao = new CRM_Core_DAO_Cache();
 
-    $result = array(); // array($path => $data)
-    while ($dao->fetch()) {
-      $result[$dao->path] = unserialize($dao->data);
+        $dao->group_name   = $group;
+        $dao->component_id = $componentID;
+        $dao->find();
+
+        $result = array(); // array($path => $data)
+        while ($dao->fetch()) {
+          $result[$dao->path] = unserialize($dao->data);
+        }
+        $dao->free();
+
+        self::$_cache[$argString] = $result;
+        $cache->set($argString, self::$_cache[$argString]);
+      }
     }
-    $dao->free();
-    return $result;
+
+    return self::$_cache[$argString];
   }
 
   /**
@@ -110,6 +143,10 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * @access public
    */
   static function setItem(&$data, $group, $path, $componentID = NULL) {
+    if (self::$_cache === NULL) {
+      self::$_cache = array();
+    }
+
     $dao = new CRM_Core_DAO_Cache();
 
     $dao->group_name   = $group;
@@ -133,6 +170,18 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     $lock->release();
 
     $dao->free();
+
+    // cache coherency - refresh or remove dependent caches
+
+    $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
+    $cache = CRM_Utils_Cache::singleton();
+    $data = unserialize($dao->data);
+    self::$_cache[$argString] = $data;
+    $cache->set($argString, $data);
+
+    $argString = "CRM_CT_CI_{$group}_{$componentID}";
+    unset(self::$_cache[$argString]);
+    $cache->delete($argString);
   }
 
   /**
@@ -140,8 +189,8 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * delete the entire cache if group is not specified
    *
    * @param string $group The group name of the entries to be deleted
-   * @param string $path  path of the item that needs to be deleted
-   * @param booleab $clearAll clear all caches
+   * @param string $path path of the item that needs to be deleted
+   * @param bool|\booleab $clearAll clear all caches
    *
    * @return void
    * @static
@@ -254,6 +303,10 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    * Do periodic cleanup of the CiviCRM session table. Also delete all session cache entries
    * which are a couple of days old. This keeps the session cache to a manageable size
    *
+   * @param bool $session
+   * @param bool $table
+   * @param bool $prevNext
+   *
    * @return void
    * @static
    * @access private