CRM-16373 - Bootstrap subsystems in more predictable order
[civicrm-core.git] / CRM / Core / BAO / Cache.php
index 031c8c2d097920437af28322ca199c4d4ddefacf..36529349710bd83ffe224e35b598d3f00075e194 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.6                                                |
+ | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  */
 
 /**
+ * BAO object for civicrm_cache table.
  *
- * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
- */
-
-/**
- * BAO object for civicrm_cache table. This is a database cache and is persisted across sessions. Typically we use
+ * This is a database cache and is persisted across sessions. Typically we use
  * this to store meta data (like profile fields, custom fields etc).
  *
  * The group_name column is used for grouping together all cache elements that logically belong to the same set.
@@ -119,7 +113,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
         $dao->component_id = $componentID;
         $dao->find();
 
-        $result = array(); // array($path => $data)
+        $result = array();
         while ($dao->fetch()) {
           $result[$dao->path] = unserialize($dao->data);
         }
@@ -144,8 +138,6 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    *   (required) The path under which this item is stored.
    * @param int $componentID
    *   The optional component ID (so componenets can share the same name space).
-   *
-   * @return void
    */
   public static function setItem(&$data, $group, $path, $componentID = NULL) {
     if (self::$_cache === NULL) {
@@ -161,8 +153,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     // get a lock so that multiple ajax requests on the same page
     // dont trample on each other
     // CRM-11234
-    $lockName = "civicrm.cache.{$group}_{$path}._{$componentID}";
-    $lock = new CRM_Core_Lock($lockName);
+    $lock = Civi::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
     if (!$lock->isAcquired()) {
       CRM_Core_Error::fatal();
     }
@@ -170,7 +161,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     $dao->find(TRUE);
     $dao->data = serialize($data);
     $dao->created_date = date('YmdHis');
-    $dao->save();
+    $dao->save(FALSE);
 
     $lock->release();
 
@@ -190,16 +181,13 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
   }
 
   /**
-   * Delete all the cache elements that belong to a group OR
-   * delete the entire cache if group is not specified
+   * Delete all the cache elements that belong to a group OR 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 bool|\booleab $clearAll clear all caches
-   *
-   * @return void
+   * @param bool $clearAll clear all caches
    */
   public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
     $dao = new CRM_Core_DAO_Cache();
@@ -231,6 +219,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
 
   /**
    * This function takes entries from the session array and stores it in the cache.
+   *
    * It also deletes the entries from the $_SESSION object (for a smaller session size)
    *
    * @param array $names
@@ -239,8 +228,6 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    *                     (in the case of profile)
    * @param bool $resetSession
    *   Should session state be reset on completion of DB store?.
-   *
-   * @return void
    */
   public static function storeSessionToCache($names, $resetSession = TRUE) {
     foreach ($names as $key => $sessionName) {
@@ -277,11 +264,11 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
    *   Array of session values that should be persisted.
    *                     This is either a form name + qfKey or just a form name
    *                     (in the case of profile)
-   *
-   * @return void
    */
 
   /**
+   * Restore session from cache.
+   *
    * @param string $names
    */
   public static function restoreSessionFromCache($names) {
@@ -306,14 +293,14 @@ 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
+   * 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
    */
   public static function cleanup($session = FALSE, $table = FALSE, $prevNext = FALSE) {
     // clean up the session cache every $cacheCleanUpNumber probabilistically
@@ -337,30 +324,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
     }
 
     if ($table) {
-      // also delete all the action temp tables
-      // that were created the same interval ago
-      $dao = new CRM_Core_DAO();
-      $query = "
-SELECT TABLE_NAME as tableName
-FROM   INFORMATION_SCHEMA.TABLES
-WHERE  TABLE_SCHEMA = %1
-AND    ( TABLE_NAME LIKE 'civicrm_task_action_temp_%'
- OR      TABLE_NAME LIKE 'civicrm_export_temp_%'
- OR      TABLE_NAME LIKE 'civicrm_import_job_%' )
-AND    CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
-";
-
-      $params = array(1 => array($dao->database(), 'String'));
-      $tableDAO = CRM_Core_DAO::executeQuery($query, $params);
-      $tables = array();
-      while ($tableDAO->fetch()) {
-        $tables[] = $tableDAO->tableName;
-      }
-      if (!empty($tables)) {
-        $table = implode(',', $tables);
-        // drop leftover temporary tables
-        CRM_Core_DAO::executeQuery("DROP TABLE $table");
-      }
+      CRM_Core_Config::clearTempTables($timeIntervalDays . ' day');
     }
 
     if ($session) {
@@ -372,7 +336,10 @@ AND    CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day )
       );
 
       $params = array(
-        1 => array(date('Y-m-d H:i:s', time() - $timeIntervalMins * 60), 'String'),
+        1 => array(
+          date('Y-m-d H:i:s', time() - $timeIntervalMins * 60),
+          'String',
+        ),
       );
       foreach ($transactionPages as $trPage) {
         $params[] = array("%${trPage}%", 'String');