Nick Lo wrote:
> I also liked the "added-value" in Rob Allen's idea...
>
>> One option would be to cache the describe (forever!) when in
>> production mode though and not cache at all in dev mode.
>
> Personally, I've used a form of DESCRIBE caching but all I did was to
> serialize the info and write it to a "database_structure" file,
> rewriting it when any changes are made to the database
As a thought exercise, supporting persistent caching with Zend_Cache in
Zend_Db_Table would mainly require:
* Adding the ability to set Zend_Cache object in the same way as you can
currently set a Zend_Db_Adapter.
* Modification of Zend_Db_Table::_setup() to look in the cache
first if $this->_cols isn't set.
Conceptually you'd then set up Zend_Db_Table within your app using:
Zend_Db_Table::setDefaultAdapter($db);
if ($liveSite == true) {
Zend_Db_Table::setCache($cache);
}
Zend_Db_Table would use "magic keys" in the $cache avoid any collisions
of the cache key. As I've now got involved in this thinking, I've
attached a completely untested patch to illustrate what I mean :)
Regards,
Rob...
Index: Table.php
===================================================================
--- Table.php (revision 1360)
+++ Table.php (working copy)
@@ -50,6 +50,13 @@
* @var Zend_Db_Adapter
*/
static protected $_defaultDb;
+
+ /**
+ * Zend_Cache object to optionally cache column data.
+ *
+ * @var Zend_Cache_Core
+ */
+ static protected $_cache;
/**
* For name inflections.
@@ -115,6 +122,24 @@
// save the connection
$this->_db = $db;
}
+
+ if (! empty($config['cache'])) {
+
+ // convenience variable
+ $cache = $config['db'];
+
+ // use an object from the registry?
+ if (is_string($cache)) {
+ $cache = Zend::registry($cache);
+ }
+
+ if (! $cache instanceof Zend_Cache_Core) {
+ throw new Zend_Db_Table_Exception('cache object does not
implement Zend_Cache_Core');
+ }
+
+ // save the cache object
+ $this->_cache = $cache;
+ }
// set the inflector
self::$_inflector = new Zend_Db_Inflector();
@@ -155,6 +180,38 @@
return $this->_db;
}
+ /**
+ * Sets the default Zend_Cache_Core for all Zend_Db_Table objects.
+ *
+ * @param Zend_Db_Adapter $db A Zend_Db_Adapter object.
+ */
+ static public final function setDefaultCache($cache)
+ {
+ // make sure it's a Zend_Cache_Core
+ if (! $cache instanceof Zend_Cache_Core) {
+ throw new Zend_Db_Table_Exception('cache object does not implement
Zend_Cache_Core');
+ }
+ Zend_Db_Table::$_cache = $cache;
+ }
+
+ /**
+ * Gets the default Zend_Cache_Core for all Zend_Db_Table objects.
+ *
+ */
+ protected final function _getDefaultCache()
+ {
+ return Zend_Db_Table::$_cache;
+ }
+
+ /**
+ * Gets the Zend_Cache_Core for this particular Zend_Db_Table object.
+ *
+ */
+ public final function getCache()
+ {
+ return $this->_cache;
+ }
+
/**
* Populate static properties for this table module.
*
@@ -177,10 +234,13 @@
}
// get the table columns
- if (! $this->_cols) {
- $tmp = array_keys($this->_db->describeTable($this->_name));
- foreach ($tmp as $native) {
- $this->_cols[$native] = self::$_inflector->camelize($native);
+ if (! $this->_cols) {
+ if(!$this->cols =
$this->cache->get('Zend_Db_Table__'.$this->_name)) {
+ $tmp = array_keys($this->_db->describeTable($this->_name));
+ foreach ($tmp as $native) {
+ $this->_cols[$native] =
self::$_inflector->camelize($native);
+ }
+ $cache->save($this->_cols, 'Zend_Db_Table__'.$this->_name);
}
}