warning('Unrecognized BAO cache group ({group}). This should work generally, but data may not be flushed in some edge-cases. Consider migrating explicitly to PSR-16.', [ 'group' => $group, ]); } $cache = CRM_Utils_Cache::create([ 'name' => "bao_$group", 'type' => array('*memory*', 'SqlGroup', 'ArrayCache'), // We're replacing CRM_Core_BAO_Cache, which traditionally used a front-cache // that was not aware of TTLs. So it seems more consistent/performant to // use 'fast' here. 'withArray' => 'fast', ]); Civi::$statics[__CLASS__][$group] = $cache; } return Civi::$statics[__CLASS__][$group]; } /** * 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 */ public static function &getItem($group, $path, $componentID = NULL) { // TODO: Generate a general deprecation notice. if ($componentID) { Civi::log() ->warning('getItem({group},{path},...) uses unsupported componentID. Consider migrating explicitly to PSR-16.', [ 'group' => $group, 'path' => $path, ]); } $value = self::getGroup($group)->get(CRM_Core_BAO_Cache::cleanKey($path)); return $value; } /** * 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). * * @throws CRM_Core_Exception */ public static function &getItems($group, $componentID = NULL) { // Based on grepping universe, this function is not currently used. // Moreover, it's hard to implement in PSR-16. (We'd have to extend the // interface.) Let's wait and see if anyone actually needs this... throw new \CRM_Core_Exception('Not implemented: CRM_Core_BAO_Cache_Psr16::getItems'); } /** * 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). */ public static function setItem(&$data, $group, $path, $componentID = NULL) { // TODO: Generate a general deprecation notice. if ($componentID) { Civi::log() ->warning('setItem({group},{path},...) uses unsupported componentID. Consider migrating explicitly to PSR-16.', [ 'group' => $group, 'path' => $path, ]); } self::getGroup($group) ->set(CRM_Core_BAO_Cache::cleanKey($path), $data, self::TTL); } /** * 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. */ public static function deleteGroup($group = NULL, $path = NULL) { // FIXME: Generate a general deprecation notice. if ($path) { self::getGroup($group)->delete(CRM_Core_BAO_Cache::cleanKey($path)); } else { self::getGroup($group)->clear(); } } /** * Cleanup any caches that we've mapped. * * Traditional SQL-backed caches are cleared as a matter of course during a * system flush (by way of "TRUNCATE TABLE civicrm_cache"). This provides * a spot where the adapter can */ public static function clearDBCache() { foreach (self::getLegacyGroups() as $groupName) { $group = self::getGroup($groupName); $group->clear(); } } /** * Get a list of known cache-groups * * @return array */ public static function getLegacyGroups() { return [ // Core 'CiviCRM Search PrevNextCache', 'contact fields', 'navigation', 'contact groups', 'custom data', // Universe 'dashboard', // be.chiro.civi.atomfeeds 'lineitem-editor', // biz.jmaconsulting.lineitemedit 'HRCore_Info', // civihr/uk.co.compucorp.civicrm.hrcore 'CiviCRM setting Spec', // nz.co.fuzion.entitysetting 'descendant groups for an org', // org.civicrm.multisite ]; } }