API CHANGE: Added DataObjectDecorator::cacheKeyComponent() to ensure that the cached behind DataObject::get_one() is appropriately specific (from r93095) (from r96749)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102378 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-04-12 02:40:50 +00:00
parent b8246bb433
commit 08075929ba
3 changed files with 32 additions and 10 deletions

View File

@ -2646,21 +2646,29 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @return DataObject The first item matching the query
*/
public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") {
$sum = md5("{$filter}_{$orderby}");
// Flush destroyed items out of the cache
if($cache && isset(DataObject::$cache_get_one[$callerClass][$sum]) && DataObject::$cache_get_one[$callerClass][$sum] instanceof DataObject && DataObject::$cache_get_one[$callerClass][$sum]->destroyed) {
DataObject::$cache_get_one[$callerClass][$sum] = false;
$SNG = singleton($callerClass);
$cacheKey = "{$filter}-{$orderby}";
if($extra = $SNG->extend('cacheKeyComponent')) {
$cacheKey .= '-' . implode("-", $extra);
}
if(!$cache || !isset(DataObject::$cache_get_one[$callerClass][$sum])) {
$item = singleton($callerClass)->instance_get_one($filter, $orderby);
$cacheKey = md5($cacheKey);
// Flush destroyed items out of the cache
if($cache && isset(DataObject::$cache_get_one[$callerClass][$cacheKey]) && DataObject::$cache_get_one[$callerClass][$cacheKey] instanceof DataObject && DataObject::$cache_get_one[$callerClass][$cacheKey]->destroyed) {
DataObject::$cache_get_one[$callerClass][$cacheKey
] = false;
}
if(!$cache || !isset(DataObject::$cache_get_one[$callerClass][$cacheKey])) {
$item = $SNG->instance_get_one($filter, $orderby);
if($cache) {
DataObject::$cache_get_one[$callerClass][$sum] = $item;
if(!DataObject::$cache_get_one[$callerClass][$sum]) {
DataObject::$cache_get_one[$callerClass][$sum] = false;
DataObject::$cache_get_one[$callerClass][$cacheKey] = $item;
if(!DataObject::$cache_get_one[$callerClass][$cacheKey]) {
DataObject::$cache_get_one[$callerClass][$cacheKey] = false;
}
}
}
return $cache ? DataObject::$cache_get_one[$callerClass][$sum] : $item;
return $cache ? DataObject::$cache_get_one[$callerClass][$cacheKey] : $item;
}
/**

View File

@ -1416,6 +1416,13 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
function getTranslatedLangs() {
return $this->getTranslatedLocales();
}
/**
* Return a piece of text to keep DataObject cache keys appropriately specific
*/
function cacheKeyComponent() {
return 'locale-'.self::get_current_locale()();
}
}

View File

@ -906,6 +906,13 @@ class Versioned extends DataObjectDecorator {
function flushCache() {
self::$cache_versionnumber = array();
}
/**
* Return a piece of text to keep DataObject cache keys appropriately specific
*/
function cacheKeyComponent() {
return 'stage-'.self::current_stage();
}
}
/**