我是Zend的初学者,在尝试缓存数据库查询结果时遇到了一些问题。
我读过很多像这样的论坛和文档(示例 31 http://framework.zend.com/manual/1.12/en/zend.db.table.html),但所有这些论坛和文档通常都说要在构造方法中设置$defaultMetadataCache。
我做到了,但似乎没有读取缓存,因为我没有得到更好的性能。
这是我对文件./library/Zend/Db/Table/Abstract的构造.php
public function __construct($config = array())
{
/**
* Allow a scalar argument to be the Adapter object or Registry key.
*/
if (!is_array($config)) {
$config = array(self::ADAPTER => $config);
}
if ($config) {
$this->setOptions($config);
}
$config_temp = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', APPLICATION_ENV);
$cache_lifetime = (isset($config_temp->cache->lifetime) && $config_temp->cache->lifetime) ? $config_temp->cache->lifetime : 300;
$cache_dir = (isset($config_temp->cache->dir) && $config_temp->cache->dir) ? $config_temp->cache->dir : '/tmp/hookit/cache/';
if(!is_dir($cache_dir))
mkdir ($cache_dir, 0755, true);
$frontendOptions = array('lifetime' => $cache_lifetime, 'automatic_serialization' => true);
$backendOptions = array('cache_dir' => $cache_dir);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
if(!self::getDefaultMetadataCache()){
self::setDefaultMetadataCache($cache);
}
$this->_setup();
$this->init();
}
$defaultMetadataCache
参数允许仅缓存表的元数据(而不是某些查询的结果)。
它是有用的,因为
默认情况下,Zend_Db_Table_Abstract查询基础数据库 表元数据,只要需要该数据来执行表,该数据 操作。
但是,一旦此缓存选项仅用于表元数据,您似乎就不应该看到性能的显着提高,就好像您将所有查询缓存到数据库一样。
此外,您还可以使用 File
作为Zend_Cache
后端。这意味着您将缓存数据存储在常规操作系统文件中,这与将数据存储在数据库中相同,数据库也使用文件来存储数据。
更好的方法(为了提高性能/减少延迟)是将缓存数据存储在 RAM 中并缓存所有查询(而不仅仅是表的元数据)。