Skip to content

Commit

Permalink
Merge pull request #17989 from eileenmcnaughton/cache_deprecate
Browse files Browse the repository at this point in the history
CRM_Core_BAO_Cache - Remove functions deprecated a year ago
  • Loading branch information
totten authored Jul 31, 2020
2 parents d92281c + 90936fd commit 3e8ffc0
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 212 deletions.
211 changes: 1 addition & 210 deletions CRM/Core/BAO/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,216 +33,6 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache {
*/
const DEFAULT_SESSION_TTL = 172800;

/**
* Cache.
*
* Format is ($cacheKey => $cacheValue)
*
* @var array
*/
public static $_cache = NULL;

/**
* Retrieve an item from the DB cache.
*
* @param string $group
* (required) The group name of the item.
* @param string $path
* (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 object
* The data if present in cache, else null
* @deprecated
*/
public static function &getItem($group, $path, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::getItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
$value = $adapter::getItem($group, $path, $componentID);
return $value;
}

if (self::$_cache === NULL) {
self::$_cache = [];
}

$argString = "CRM_CT_{$group}_{$path}_{$componentID}";
if (!array_key_exists($argString, self::$_cache)) {
$cache = CRM_Utils_Cache::singleton();
$cleanKey = self::cleanKey($argString);
self::$_cache[$argString] = $cache->get($cleanKey);
if (self::$_cache[$argString] === NULL) {
$table = self::getTableName();
$where = self::whereCache($group, $path, $componentID);
$rawData = CRM_Core_DAO::singleValueQuery("SELECT data FROM $table WHERE $where");
$data = $rawData ? self::decode($rawData) : NULL;

self::$_cache[$argString] = $data;
if ($data !== NULL) {
// Do not cache 'null' as that is most likely a cache miss & we shouldn't then cache it.
$cache->set($cleanKey, self::$_cache[$argString]);
}
}
}
return self::$_cache[$argString];
}

/**
* Retrieve all items in a group.
*
* @param string $group
* (required) The group name of the item.
* @param int $componentID
* The optional component ID (so componenets can share the same name space).
*
* @return object
* The data if present in cache, else null
* @deprecated
*/
public static function &getItems($group, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::getItems is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::getItems($group, $componentID);
}

if (self::$_cache === NULL) {
self::$_cache = [];
}

$argString = "CRM_CT_CI_{$group}_{$componentID}";
if (!array_key_exists($argString, self::$_cache)) {
$cache = CRM_Utils_Cache::singleton();
$cleanKey = self::cleanKey($argString);
self::$_cache[$argString] = $cache->get($cleanKey);
if (!self::$_cache[$argString]) {
$table = self::getTableName();
$where = self::whereCache($group, NULL, $componentID);
$dao = CRM_Core_DAO::executeQuery("SELECT path, data FROM $table WHERE $where");

$result = [];
while ($dao->fetch()) {
$result[$dao->path] = self::decode($dao->data);
}

self::$_cache[$argString] = $result;
$cache->set($cleanKey, self::$_cache[$argString]);
}
}

return self::$_cache[$argString];
}

/**
* Store an item in the DB cache.
*
* @param object $data
* (required) A reference to the data that will be serialized and stored.
* @param string $group
* (required) The group name of the item.
* @param string $path
* (required) The path under which this item is stored.
* @param int $componentID
* The optional component ID (so componenets can share the same name space).
* @deprecated
* @throws CRM_Core_Exception
*/
public static function setItem(&$data, $group, $path, $componentID = NULL) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::setItem is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::setItem($data, $group, $path, $componentID);
}

if (self::$_cache === NULL) {
self::$_cache = [];
}

// get a lock so that multiple ajax requests on the same page
// dont trample on each other
// CRM-11234
$lock = Civi::lockManager()->acquire("cache.{$group}_{$path}._{$componentID}");
if (!$lock->isAcquired()) {
throw new CRM_Core_Exception('Cannot acquire database lock');
}

$table = self::getTableName();
$where = self::whereCache($group, $path, $componentID);
$dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM $table WHERE {$where}");
// FIXME - Use SQL NOW() or CRM_Utils_Time?
$now = date('Y-m-d H:i:s');
$dataSerialized = self::encode($data);

// This table has a wonky index, so we cannot use REPLACE or
// "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE).
if ($dataExists) {
$sql = "UPDATE $table SET data = %1, created_date = %2 WHERE {$where}";
$args = [
1 => [$dataSerialized, 'String'],
2 => [$now, 'String'],
];
$dao = CRM_Core_DAO::executeQuery($sql, $args, TRUE, NULL, FALSE, FALSE);
}
else {
$insert = CRM_Utils_SQL_Insert::into($table)
->row([
'group_name' => $group,
'path' => $path,
'component_id' => $componentID,
'data' => $dataSerialized,
'created_date' => $now,
]);
$dao = CRM_Core_DAO::executeQuery($insert->toSQL(), [], TRUE, NULL, FALSE, FALSE);
}

$lock->release();

// cache coherency - refresh or remove dependent caches

$argString = "CRM_CT_{$group}_{$path}_{$componentID}";
$cache = CRM_Utils_Cache::singleton();
$data = self::decode($dataSerialized);
self::$_cache[$argString] = $data;
$cache->set(self::cleanKey($argString), $data);

$argString = "CRM_CT_CI_{$group}_{$componentID}";
unset(self::$_cache[$argString]);
$cache->delete(self::cleanKey($argString));
}

/**
* 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 $clearAll clear all caches
* @deprecated
*/
public static function deleteGroup($group = NULL, $path = NULL, $clearAll = TRUE) {
CRM_Core_Error::deprecatedFunctionWarning(
'CRM_Core_BAO_Cache::deleteGroup is deprecated and will be removed from core soon, use Civi::cache() facade or define cache group using hook_civicrm_container'
);
if (($adapter = CRM_Utils_Constant::value('CIVICRM_BAO_CACHE_ADAPTER')) !== NULL) {
return $adapter::deleteGroup($group, $path);
}
else {
$table = self::getTableName();
$where = self::whereCache($group, $path, NULL);
CRM_Core_DAO::executeQuery("DELETE FROM $table WHERE $where");
}

if ($clearAll) {
self::resetCaches();
}
}

/**
* Cleanup ACL and System Level caches
*/
Expand Down Expand Up @@ -471,6 +261,7 @@ protected static function whereCache($group, $path, $componentID) {
* @see CRM_Utils_Cache::cleanKey()
*/
public static function cleanKey($key) {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Utils_Cache::cleanKey');
return CRM_Utils_Cache::cleanKey($key);
}

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ public static function flushCache() {
= CRM_Contribute_BAO_Contribution::$_exportableFields
= CRM_Pledge_BAO_Pledge::$_exportableFields
= CRM_Core_BAO_CustomField::$_importFields
= CRM_Core_BAO_Cache::$_cache = CRM_Core_DAO::$_dbColumnValueCache = NULL;
= CRM_Core_DAO::$_dbColumnValueCache = NULL;

CRM_Core_OptionGroup::flushAll();
CRM_Utils_PseudoConstant::flushAll();
Expand Down
1 change: 0 additions & 1 deletion tests/phpunit/CRM/Core/BAO/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function testSetGetItem($originalValue) {
// Wipe out any in-memory copies of the cache. Check to see if the SQL
// read is correct.

CRM_Core_BAO_Cache::$_cache = NULL;
CRM_Utils_Cache::$_singleton = NULL;
$this->a->values = [];
$return_2 = $this->a->get('testSetGetItem');
Expand Down

0 comments on commit 3e8ffc0

Please sign in to comment.