summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/TActiveRecordGateway.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/ActiveRecord/TActiveRecordGateway.php')
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php62
1 files changed, 47 insertions, 15 deletions
diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php
index 19c8552f..e7ea5e46 100644
--- a/framework/Data/ActiveRecord/TActiveRecordGateway.php
+++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php
@@ -22,7 +22,7 @@
class TActiveRecordGateway extends TComponent
{
private $_manager;
- private $_tables=array();
+ private $_tables=array(); //meta data cache.
/**
* Property name for optional table name in TActiveRecord.
@@ -68,35 +68,67 @@ class TActiveRecordGateway extends TComponent
}
/**
- * Gets the meta data for given database and table.
+ * @param TActiveRecord active record.
+ * @return TDbMetaData table meta data, null if not found.
*/
- public function getMetaData(TActiveRecord $record)
+ protected function getCachedMetaData($record)
{
$type=get_class($record);
- if(!isset($this->_tables[$type]))
+ if(isset($this->_tables[$type]))
+ return $this->_tables[$type];
+ if(($cache=$this->getManager()->getCache())!==null)
{
- $conn = $record->getDbConnection();
- $inspector = $this->getManager()->getTableInspector($conn);
- $table = $this->getTableName($record);
- $this->_tables[$type] = $inspector->getTableMetaData($table);
+ //force loading of the table inspector to load the required classes
+ // before unserializing cached meta data.
+ $this->getManager()->getTableInspector($record->getDbConnection());
+ $data = $cache->get($this->getMetaDataCacheKey($record));
+ if($data !== false && $data !== null)
+ {
+ $this->_tables[$type] = $data;
+ return $data;
+ }
}
- return $this->_tables[$type];
}
/**
- * @param array table meta data.
+ * @param TActiveRecord active record.
+ * @return string cache key, using connection string + record class name
+ */
+ protected function getMetaDataCacheKey($record)
+ {
+ $conn = $record->getDbConnection()->getConnectionString();
+ return $conn.':'.get_class($record);
+ }
+
+ /**
+ * Cache the meta data, tries the application cache if applicable.
+ * @param TActiveRecord active record.
+ * @param TDbMetaData table meta data
+ * @return TDbMetaData table meta data.
*/
- public function setAllMetaData($data)
+ protected function cacheMetaData($record,$data)
{
- $this->_tables=$data;
+ $type = get_class($record);
+ if(($cache=$this->getManager()->getCache())!==null)
+ $cache->set($this->getMetaDataCacheKey($record), $data);
+ $this->_tables[$type] = $data;
+ return $data;
}
/**
- * @return array all table meta data.
+ * Gets the meta data for given database and table.
*/
- public function getAllMetaData()
+ public function getMetaData(TActiveRecord $record)
{
- return $this->_tables;
+ $type=get_class($record);
+ if(!($data = $this->getCachedMetaData($record)))
+ {
+ $conn = $record->getDbConnection();
+ $inspector = $this->getManager()->getTableInspector($conn);
+ $table = $this->getTableName($record);
+ $data = $this->cacheMetaData($record,$inspector->getTableMetaData($table));
+ }
+ return $data;
}
/**