-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(dev/core#635) Deprecate CRM_Core_BAO_Cache for I/O. Optionally redirect I/O to Redis or Memcache. #13489
Merged
Merged
(dev/core#635) Deprecate CRM_Core_BAO_Cache for I/O. Optionally redirect I/O to Redis or Memcache. #13489
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5a302bb
Allow rerouting CRM_Core_BAO_Cache::{set,get}Item(s) to PSR-16 drivers
totten 920fa38
CRM_Core_BAO_Cache - Deprecate getItems(), getItem(), setItem(), dele…
totten 88dfeb5
CRM_Core_BAO_Cache - Make some IDE's happier about inline assignment
totten b5e7d57
CRM_Core_BAO_Cache - When delegating to an adapter, don't expect it t…
totten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| CiviCRM version 5 | | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC (c) 2004-2019 | | ||
+--------------------------------------------------------------------+ | ||
| This file is a part of CiviCRM. | | ||
| | | ||
| CiviCRM is free software; you can copy, modify, and distribute it | | ||
| under the terms of the GNU Affero General Public License | | ||
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | ||
| | | ||
| CiviCRM is distributed in the hope that it will be useful, but | | ||
| WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | ||
| See the GNU Affero General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU Affero General Public | | ||
| License and the CiviCRM Licensing Exception along | | ||
| with this program; if not, contact CiviCRM LLC | | ||
| at info[AT]civicrm[DOT]org. If you have questions about the | | ||
| GNU Affero General Public License or the licensing of CiviCRM, | | ||
| see the CiviCRM license FAQ at http://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* Class CRM_Core_BAO_Cache_Psr16 | ||
* | ||
* This optional adapter to help phase-out CRM_Core_BAO_Cache. | ||
* | ||
* In effect, it changes the default behavior of legacy cache-consumers | ||
* (CRM_Core_BAO_Cache) so that they store in the best-available tier | ||
* (Reds/Memcache or SQL or array) rather than being hard-coded to SQL. | ||
* | ||
* It basically just calls "CRM_Utils_Cache::create()" for each $group and | ||
* maps the getItem/setItem to get()/set(). | ||
*/ | ||
class CRM_Core_BAO_Cache_Psr16 { | ||
|
||
/** | ||
* Original BAO behavior did not do expiration. PSR-16 providers have | ||
* diverse defaults. To provide some consistency, we'll pick a long(ish) | ||
* TTL for everything that goes through the adapter. | ||
*/ | ||
const TTL = 86400; | ||
|
||
/** | ||
* @param string $group | ||
* @return CRM_Utils_Cache_Interface | ||
*/ | ||
protected static function getGroup($group) { | ||
if (!isset(Civi::$statics[__CLASS__][$group])) { | ||
if (!in_array($group, self::getLegacyGroups())) { | ||
Civi::log() | ||
->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, | ||
]); | ||
} | ||
return self::getGroup($group)->get(CRM_Core_BAO_Cache::cleanKey($path)); | ||
} | ||
|
||
/** | ||
* 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 | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could remove this entitysetting one - I consider that replaced by Custom Data on any