Skip to content
This repository
Browse code

BUGFIX: make internal DataObject caches more consistent (os6175)

  • Loading branch information...
commit 8ea2460e50b7a5cd8c29c019c8f6b6a839c33b86 1 parent 621b2e1
Mateusz U authored April 12, 2012

Showing 1 changed file with 35 additions and 64 deletions. Show diff stats Hide diff stats

  1. 99  model/DataObject.php
99  model/DataObject.php
@@ -85,11 +85,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
85 85
 	 * @todo Define the options that can be set here
86 86
 	 */
87 87
 	public static $api_access = false;
88  
-	
89  
-	public static
90  
-		$cache_has_own_table       = array(),
91  
-		$cache_has_own_table_field = array();
92  
-	
  88
+
93 89
 	/**
94 90
 	 * True if this DataObject has been destroyed.
95 91
 	 * @var boolean
@@ -128,13 +124,6 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
128 124
 	protected $original;
129 125
 
130 126
 	/**
131  
-	 * The one-to-one, one-to-many and many-to-one components
132  
-	 * indexed by component name.
133  
-	 * @var array
134  
-	 */
135  
-	protected $components;
136  
-	
137  
-	/**
138 127
 	 * Used by onBeforeDelete() to ensure child classes call parent::onBeforeDelete()
139 128
 	 * @var boolean
140 129
 	 */
@@ -150,7 +139,21 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
150 139
 	 * Should dataobjects be validated before they are written?
151 140
 	 */
152 141
 	private static $validation_enabled = true;
153  
-	
  142
+
  143
+	/**
  144
+	 * Static caches used by relevant functions.
  145
+	 */
  146
+	public static $cache_has_own_table = array();
  147
+	public static $cache_has_own_table_field = array();
  148
+	protected static $_cache_get_one;
  149
+	protected static $_cache_get_class_ancestry;
  150
+	protected static $_cache_composite_fields = array();	
  151
+
  152
+	/**
  153
+	 * Non-static relationship cache, indexed by component name.
  154
+	 */
  155
+	protected $components;
  156
+
154 157
 	/**
155 158
 	 * Returns when validation on DataObjects is enabled.
156 159
 	 * @return bool
@@ -227,9 +230,6 @@ public static function custom_database_fields($class) {
227 230
 		return (array)$fields;
228 231
 	}
229 232
 	
230  
-	private static $_cache_custom_database_fields = array();
231  
-	
232  
-	
233 233
 	/**
234 234
 	 * Returns the field class if the given db field on the class is a composite field.
235 235
 	 * Will check all applicable ancestor classes and aggregate results.
@@ -283,9 +283,6 @@ private static function cache_composite_fields($class) {
283 283
 		self::$_cache_composite_fields[$class] = $compositeFields;
284 284
 	}
285 285
 	
286  
-	private static $_cache_composite_fields = array();
287  
-	
288  
-
289 286
 	/**
290 287
 	 * Construct a new DataObject.
291 288
 	 *
@@ -391,7 +388,7 @@ function setModel(DataModel $model) {
391 388
 	}
392 389
 
393 390
 	/**
394  
-	 * Destroy all of this objects dependant objects.
  391
+	 * Destroy all of this objects dependant objects and local caches.
395 392
 	 * You'll need to call this to get the memory of an object that has components or extensions freed.
396 393
 	 */
397 394
 	function destroy() {
@@ -1159,10 +1156,9 @@ public function write($showDebug = false, $forceInsert = false, $forceWrite = fa
1159 1156
 		return $this->record['ID'];
1160 1157
 	}
1161 1158
 
1162  
-
1163 1159
 	/**
1164 1160
 	 * Write the cached components to the database. Cached components could refer to two different instances of the same record.
1165  
-	 * 
  1161
+	 *
1166 1162
 	 * @param $recursive Recursively write components
1167 1163
 	 */
1168 1164
 	public function writeComponents($recursive = false) {
@@ -1225,12 +1221,6 @@ public static function delete_by_id($className, $id) {
1225 1221
 	}
1226 1222
 
1227 1223
 	/**
1228  
-	 * A cache used by getClassAncestry()
1229  
-	 * @var array
1230  
-	 */
1231  
-	protected static $ancestry;
1232  
-
1233  
-	/**
1234 1224
 	 * Get the class ancestry, including the current class name.
1235 1225
 	 * The ancestry will be returned as an array of class names, where the 0th element
1236 1226
 	 * will be the class that inherits directly from DataObject, and the last element
@@ -1239,13 +1229,13 @@ public static function delete_by_id($className, $id) {
1239 1229
 	 * @return array Class ancestry
1240 1230
 	 */
1241 1231
 	public function getClassAncestry() {
1242  
-		if(!isset(DataObject::$ancestry[$this->class])) {
1243  
-			DataObject::$ancestry[$this->class] = array($this->class);
1244  
-			while(($class = get_parent_class(DataObject::$ancestry[$this->class][0])) != "DataObject") {
1245  
-				array_unshift(DataObject::$ancestry[$this->class], $class);
  1232
+		if(!isset(DataObject::$_cache_get_class_ancestry[$this->class])) {
  1233
+			DataObject::$_cache_get_class_ancestry[$this->class] = array($this->class);
  1234
+			while(($class = get_parent_class(DataObject::$_cache_get_class_ancestry[$this->class][0])) != "DataObject") {
  1235
+				array_unshift(DataObject::$_cache_get_class_ancestry[$this->class], $class);
1246 1236
 			}
1247 1237
 		}
1248  
-		return DataObject::$ancestry[$this->class];
  1238
+		return DataObject::$_cache_get_class_ancestry[$this->class];
1249 1239
 	}
1250 1240
 
1251 1241
 	/**
@@ -1293,12 +1283,6 @@ public function getComponent($componentName) {
1293 1283
 	}
1294 1284
 
1295 1285
 	/**
1296  
-	 * A cache used by component getting classes
1297  
-	 * @var array
1298  
-	 */
1299  
-	protected $componentCache;
1300  
-
1301  
-	/**
1302 1286
 	 * Returns a one-to-many relation as a HasManyList
1303 1287
 	 *
1304 1288
 	 * @param string $componentName Name of the component
@@ -2487,11 +2471,6 @@ public function buildSQL($filter = "", $sort = "", $limit = "", $join = "", $res
2487 2471
 		return $this->extendedSQL($filter, $sort, $limit, $join, $having);
2488 2472
 
2489 2473
 	}
2490  
-	
2491  
-	/**
2492  
-	 * Cache for the hairy bit of buildSQL
2493  
-	 */
2494  
-	private static $cache_buildSQL_query;
2495 2474
 
2496 2475
 	/**
2497 2476
 	 * @deprecated 3.0 Use DataList::create and DataList to do your querying
@@ -2612,12 +2591,6 @@ function buildDataObjectSet($records, $containerClass = "DataObjectSet", $query
2612 2591
 	}
2613 2592
 
2614 2593
 	/**
2615  
-	 * A cache used by get_one.
2616  
-	 * @var array
2617  
-	 */
2618  
-	protected static $cache_get_one;
2619  
-
2620  
-	/**
2621 2594
 	 * Return the first item matching the given query.
2622 2595
 	 * All calls to get_one() are cached.
2623 2596
 	 *
@@ -2638,28 +2611,28 @@ public static function get_one($callerClass, $filter = "", $cache = true, $order
2638 2611
 		$cacheKey = md5($cacheKey);
2639 2612
 		
2640 2613
 		// Flush destroyed items out of the cache
2641  
-		if($cache && isset(DataObject::$cache_get_one[$callerClass][$cacheKey]) && DataObject::$cache_get_one[$callerClass][$cacheKey] instanceof DataObject && DataObject::$cache_get_one[$callerClass][$cacheKey]->destroyed) {
2642  
-			DataObject::$cache_get_one[$callerClass][$cacheKey
  2614
+		if($cache && isset(DataObject::$_cache_get_one[$callerClass][$cacheKey]) && DataObject::$_cache_get_one[$callerClass][$cacheKey] instanceof DataObject && DataObject::$_cache_get_one[$callerClass][$cacheKey]->destroyed) {
  2615
+			DataObject::$_cache_get_one[$callerClass][$cacheKey
2643 2616
 			] = false;
2644 2617
 		}
2645  
-		if(!$cache || !isset(DataObject::$cache_get_one[$callerClass][$cacheKey])) {
  2618
+		if(!$cache || !isset(DataObject::$_cache_get_one[$callerClass][$cacheKey])) {
2646 2619
 			$dl = DataList::create($callerClass)->where($filter)->sort($orderby);
2647 2620
 			$dl->setModel(DataModel::inst());
2648 2621
 			$item = $dl->First();
2649 2622
 
2650 2623
 			if($cache) {
2651  
-				DataObject::$cache_get_one[$callerClass][$cacheKey] = $item;
2652  
-				if(!DataObject::$cache_get_one[$callerClass][$cacheKey]) {
2653  
-					DataObject::$cache_get_one[$callerClass][$cacheKey] = false;
  2624
+				DataObject::$_cache_get_one[$callerClass][$cacheKey] = $item;
  2625
+				if(!DataObject::$_cache_get_one[$callerClass][$cacheKey]) {
  2626
+					DataObject::$_cache_get_one[$callerClass][$cacheKey] = false;
2654 2627
 				}
2655 2628
 			}
2656 2629
 		}
2657  
-		return $cache ? DataObject::$cache_get_one[$callerClass][$cacheKey] : $item;
  2630
+		return $cache ? DataObject::$_cache_get_one[$callerClass][$cacheKey] : $item;
2658 2631
 	}
2659 2632
 
2660 2633
 	/**
2661 2634
 	 * Flush the cached results for all relations (has_one, has_many, many_many)
2662  
-	 * Also clears any cached aggregate data
  2635
+	 * Also clears any cached aggregate data.
2663 2636
 	 *
2664 2637
 	 * @param boolean $persistent When true will also clear persistent data stored in the Cache system.
2665 2638
 	 *                            When false will just clear session-local cached data 
@@ -2669,27 +2642,25 @@ public function flushCache($persistent = true) {
2669 2642
 		if($persistent) Aggregate::flushCache($this->class);
2670 2643
 		
2671 2644
 		if($this->class == 'DataObject') {
2672  
-			DataObject::$cache_get_one = array();
  2645
+			DataObject::$_cache_get_one = array();
2673 2646
 			return;
2674 2647
 		}
2675 2648
 
2676 2649
 		$classes = ClassInfo::ancestry($this->class);
2677 2650
 		foreach($classes as $class) {
2678  
-			if(isset(self::$cache_get_one[$class])) unset(self::$cache_get_one[$class]);
  2651
+			if(isset(self::$_cache_get_one[$class])) unset(self::$_cache_get_one[$class]);
2679 2652
 		}
2680 2653
 		
2681 2654
 		$this->extend('flushCache');
2682  
-		
2683  
-		$this->componentCache = array();
2684 2655
 	}
2685 2656
 
2686 2657
 	static function flush_and_destroy_cache() {
2687  
-		if(self::$cache_get_one) foreach(self::$cache_get_one as $class => $items) {
  2658
+		if(self::$_cache_get_one) foreach(self::$_cache_get_one as $class => $items) {
2688 2659
 			if(is_array($items)) foreach($items as $item) {
2689 2660
 				if($item) $item->destroy();
2690 2661
 			}
2691 2662
 		}
2692  
-		self::$cache_get_one = array();
  2663
+		self::$_cache_get_one = array();
2693 2664
 	}
2694 2665
 	
2695 2666
 	/**

0 notes on commit 8ea2460

Please sign in to comment.
Something went wrong with that request. Please try again.