Skip tags in Zend_Cache for Aggregate if not available

Allows usage of one consistent Zend_Cache backend
for all SilverStripe core storage, greatly simplifying
its configuration. This means a call to Aggregate::flushCache()
will indeed flush all caches if the backend doesn't support tags,
including any custom caches defined through SS_Cache.
Given caches are regarded transient that's an acceptable limitation.
This commit is contained in:
Ingo Schommer 2013-11-21 12:18:29 +01:00
parent 10e184fd31
commit cc51e0a889

View File

@ -43,19 +43,24 @@ class Aggregate extends ViewableData {
protected static function cache() { protected static function cache() {
return self::$cache ? self::$cache : (self::$cache = SS_Cache::factory('aggregate')); return self::$cache ? self::$cache : (self::$cache = SS_Cache::factory('aggregate'));
} }
/** Clear the aggregate cache for a given type, or pass nothing to clear all aggregate caches */ /**
* Clear the aggregate cache for a given type, or pass nothing to clear all aggregate caches.
* {@link $class} is just effective if the cache backend supports tags.
*/
public static function flushCache($class=null) { public static function flushCache($class=null) {
$cache = self::cache(); $cache = self::cache();
$capabilities = $cache->getBackend()->getCapabilities();
if (!$class || $class == 'DataObject') { if($capabilities['tags'] && (!$class || $class == 'DataObject')) {
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('aggregate')); $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('aggregate'));
} else { } elseif($capabilities['tags']) {
$tags = ClassInfo::ancestry($class); $tags = ClassInfo::ancestry($class);
foreach($tags as &$tag) { foreach($tags as &$tag) {
$tag = preg_replace('/[^a-zA-Z0-9_]/', '_', $tag); $tag = preg_replace('/[^a-zA-Z0-9_]/', '_', $tag);
} }
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags); $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
} else {
$cache->clean(Zend_Cache::CLEANING_MODE_ALL);
} }
} }