/** * Removes all the cache entries pertaining to a specific group. * If no groupID is passed in, removes cache entries for all groups * Has an optimization to bypass repeated invocations of this function. * Note that this function is an advisory, i.e. the removal respects the * cache date, i.e. the removal is not done if the group was recently * loaded into the cache. * * @param int $groupID * the groupID to delete cache entries, NULL for all groups. * @param bool $onceOnly * run the function exactly once for all groups. * * @return void */ public static function remove($groupID = NULL, $onceOnly = TRUE) { static $invoked = FALSE; // typically this needs to happy only once per instance // this is especially TRUE in import, where we dont need // to do this all the time // this optimization is done only when no groupID is passed // i.e. cache is reset for all groups if ($onceOnly && $invoked && $groupID == NULL) { return; } if ($groupID == NULL) { $invoked = TRUE; } elseif (is_array($groupID)) { foreach ($groupID as $gid) { unset(self::$_alreadyLoaded[$gid]); } } elseif ($groupID && array_key_exists($groupID, self::$_alreadyLoaded)) { unset(self::$_alreadyLoaded[$groupID]); } $refresh = NULL; $params = array(); $smartGroupCacheTimeout = self::smartGroupCacheTimeout(); $now = CRM_Utils_Date::getUTCTime(); $refreshTime = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60); if (!isset($groupID)) { if ($smartGroupCacheTimeout == 0) { $query = "\nTRUNCATE civicrm_group_contact_cache\n"; $update = "\nUPDATE civicrm_group g\nSET cache_date = null,\n refresh_date = null\n"; } else { $query = "\nDELETE gc\nFROM civicrm_group_contact_cache gc\nINNER JOIN civicrm_group g ON g.id = gc.group_id\nWHERE TIMESTAMPDIFF(MINUTE, g.cache_date, {$now}) >= {$smartGroupCacheTimeout}\n"; $update = "\nUPDATE civicrm_group g\nSET cache_date = null,\n refresh_date = null\nWHERE TIMESTAMPDIFF(MINUTE, cache_date, {$now}) >= {$smartGroupCacheTimeout}\n"; $refresh = "\nUPDATE civicrm_group g\nSET refresh_date = {$refreshTime}\nWHERE TIMESTAMPDIFF(MINUTE, cache_date, {$now}) < {$smartGroupCacheTimeout}\nAND refresh_date IS NULL\n"; } } elseif (is_array($groupID)) { $groupIDs = implode(', ', $groupID); $query = "\nDELETE g\nFROM civicrm_group_contact_cache g\nWHERE g.group_id IN ( {$groupIDs} )\n"; $update = "\nUPDATE civicrm_group g\nSET cache_date = null,\n refresh_date = null\nWHERE id IN ( {$groupIDs} )\n"; } else { $query = "\nDELETE g\nFROM civicrm_group_contact_cache g\nWHERE g.group_id = %1\n"; $update = "\nUPDATE civicrm_group g\nSET cache_date = null,\n refresh_date = null\nWHERE id = %1\n"; $params = array(1 => array($groupID, 'Integer')); } CRM_Core_DAO::executeQuery($query, $params); if ($refresh) { CRM_Core_DAO::executeQuery($refresh, $params); } // also update the cache_date for these groups CRM_Core_DAO::executeQuery($update, $params); }
/** * Deletes all the cache entries. */ public static function resetCache() { // reset any static caching self::$_cache = NULL; // reset any db caching $config = CRM_Core_Config::singleton(); $smartGroupCacheTimeout = CRM_Contact_BAO_GroupContactCache::smartGroupCacheTimeout(); //make sure to give original timezone settings again. $now = CRM_Utils_Date::getUTCTime(); $query = "\nDELETE\nFROM civicrm_acl_cache\nWHERE modified_date IS NULL\n OR (TIMESTAMPDIFF(MINUTE, modified_date, {$now}) >= {$smartGroupCacheTimeout})\n"; CRM_Core_DAO::singleValueQuery($query); // CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); // No, force-commits transaction // CRM_Core_DAO::singleValueQuery("DELETE FROM civicrm_acl_contact_cache"); // Transaction-safe if (CRM_Core_Transaction::isActive()) { CRM_Core_Transaction::addCallback(CRM_Core_Transaction::PHASE_POST_COMMIT, function () { CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); }); } else { CRM_Core_DAO::singleValueQuery("TRUNCATE TABLE civicrm_acl_contact_cache"); } }