mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #3985 from tractorcow/pulls/3.2/refactor-obj-cache
Refactor ViewableData::obj caching
This commit is contained in:
commit
3b9129f681
@ -156,6 +156,25 @@ class ViewableDataTest extends SapphireTest {
|
|||||||
|
|
||||||
$this->assertEquals($uncastedData, $castedData->getValue(), 'Casted and uncasted strings are not equal.');
|
$this->assertEquals($uncastedData, $castedData->getValue(), 'Casted and uncasted strings are not equal.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCaching() {
|
||||||
|
$objCached = new ViewableDataTest_Cached();
|
||||||
|
$objNotCached = new ViewableDataTest_NotCached();
|
||||||
|
|
||||||
|
$objCached->Test = 'AAA';
|
||||||
|
$objNotCached->Test = 'AAA';
|
||||||
|
|
||||||
|
$this->assertEquals('AAA', $objCached->obj('Test', null, true, true));
|
||||||
|
$this->assertEquals('AAA', $objNotCached->obj('Test', null, true, true));
|
||||||
|
|
||||||
|
$objCached->Test = 'BBB';
|
||||||
|
$objNotCached->Test = 'BBB';
|
||||||
|
|
||||||
|
// Cached data must be always the same
|
||||||
|
$this->assertEquals('AAA', $objCached->obj('Test', null, true, true));
|
||||||
|
$this->assertEquals('BBB', $objNotCached->obj('Test', null, true, true));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**#@+
|
/**#@+
|
||||||
@ -253,4 +272,17 @@ class ViewableDataTest_NoCastingInformation extends ViewableData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ViewableDataTest_Cached extends ViewableData {
|
||||||
|
public $Test;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewableDataTest_NotCached extends ViewableData {
|
||||||
|
public $Test;
|
||||||
|
|
||||||
|
protected function objCacheGet($key) {
|
||||||
|
// Disable caching
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**#@-*/
|
/**#@-*/
|
||||||
|
@ -346,6 +346,38 @@ class ViewableData extends Object implements IteratorAggregate {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate the cache name for a field
|
||||||
|
*
|
||||||
|
* @param string $fieldName Name of field
|
||||||
|
* @param array $arguments List of optional arguments given
|
||||||
|
*/
|
||||||
|
protected function objCacheName($fieldName, $arguments) {
|
||||||
|
return $arguments
|
||||||
|
? $fieldName . ":" . implode(',', $arguments)
|
||||||
|
: $fieldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a cached value from the field cache
|
||||||
|
*
|
||||||
|
* @param string $key Cache key
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function objCacheGet($key) {
|
||||||
|
if(isset($this->objCache[$key])) return $this->objCache[$key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a value in the field cache
|
||||||
|
*
|
||||||
|
* @param string $key Cache key
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
protected function objCacheSet($key, $value) {
|
||||||
|
$this->objCache[$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value of a field on this object, automatically inserting the value into any available casting objects
|
* Get the value of a field on this object, automatically inserting the value into any available casting objects
|
||||||
* that have been specified.
|
* that have been specified.
|
||||||
@ -354,12 +386,14 @@ class ViewableData extends Object implements IteratorAggregate {
|
|||||||
* @param array $arguments
|
* @param array $arguments
|
||||||
* @param bool $forceReturnedObject if TRUE, the value will ALWAYS be casted to an object before being returned,
|
* @param bool $forceReturnedObject if TRUE, the value will ALWAYS be casted to an object before being returned,
|
||||||
* even if there is no explicit casting information
|
* even if there is no explicit casting information
|
||||||
|
* @param bool $cache Cache this object
|
||||||
* @param string $cacheName a custom cache name
|
* @param string $cacheName a custom cache name
|
||||||
*/
|
*/
|
||||||
public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) {
|
public function obj($fieldName, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) {
|
||||||
if(!$cacheName) $cacheName = $arguments ? $fieldName . implode(',', $arguments) : $fieldName;
|
if(!$cacheName && $cache) $cacheName = $this->objCacheName($fieldName, $arguments);
|
||||||
|
|
||||||
if(!isset($this->objCache[$cacheName])) {
|
$value = $cache ? $this->objCacheGet($cacheName) : null;
|
||||||
|
if(!isset($value)) {
|
||||||
// HACK: Don't call the deprecated FormField::Name() method
|
// HACK: Don't call the deprecated FormField::Name() method
|
||||||
$methodIsAllowed = true;
|
$methodIsAllowed = true;
|
||||||
if($this instanceof FormField && $fieldName == 'Name') $methodIsAllowed = false;
|
if($this instanceof FormField && $fieldName == 'Name') $methodIsAllowed = false;
|
||||||
@ -381,9 +415,7 @@ class ViewableData extends Object implements IteratorAggregate {
|
|||||||
$value = $valueObject;
|
$value = $valueObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($cache) $this->objCache[$cacheName] = $value;
|
if($cache) $this->objCacheSet($cacheName, $value);
|
||||||
} else {
|
|
||||||
$value = $this->objCache[$cacheName];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!is_object($value) && $forceReturnedObject) {
|
if(!is_object($value) && $forceReturnedObject) {
|
||||||
|
Loading…
Reference in New Issue
Block a user