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 * @return DataObject The first item matching the query
*/ */
public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") { public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "") {
$sum = md5("{$filter}_{$orderby}"); $SNG = singleton($callerClass);
// 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) { $cacheKey = "{$filter}-{$orderby}";
DataObject::$cache_get_one[$callerClass][$sum] = false; if($extra = $SNG->extend('cacheKeyComponent')) {
$cacheKey .= '-' . implode("-", $extra);
} }
if(!$cache || !isset(DataObject::$cache_get_one[$callerClass][$sum])) { $cacheKey = md5($cacheKey);
$item = singleton($callerClass)->instance_get_one($filter, $orderby);
// 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) { if($cache) {
DataObject::$cache_get_one[$callerClass][$sum] = $item; DataObject::$cache_get_one[$callerClass][$cacheKey] = $item;
if(!DataObject::$cache_get_one[$callerClass][$sum]) { if(!DataObject::$cache_get_one[$callerClass][$cacheKey]) {
DataObject::$cache_get_one[$callerClass][$sum] = false; 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() { function getTranslatedLangs() {
return $this->getTranslatedLocales(); 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() { function flushCache() {
self::$cache_versionnumber = array(); self::$cache_versionnumber = array();
} }
/**
* Return a piece of text to keep DataObject cache keys appropriately specific
*/
function cacheKeyComponent() {
return 'stage-'.self::current_stage();
}
} }
/** /**