我正在尝试使用Zend_Cache_Frontend_Function
来缓存我自己的一些函数调用。
缓存的设置如下:
$manager = new Zend_Cache_Manager();
$frontendOptions = array(
'lifetime' => intval(CACHELIFETIME),
'automatic_serialization' => true);
$backendOptions = array('cache_dir' => CACHEDIR);
$funcCache = Zend_Cache::factory('Function',
'File',
$frontendOptions,
$backendOptions);
$manager->setCache('function', $funcCache);
然后我就像这样使用缓存:
$country_name = "UK";
$country_id = 1;
$country = new Default_Db_Table_Country();
$country = $cache->call(array($country, "getCountryByName"), array($country_name, $id));
即使在多次调用之后,仍然会调用函数getCountryByName()
并且其中的数据库查询会运行。
我是否正确使用此功能?如何多次停止对getCountryByName()
的调用?
由于
修改
查看源代码,似乎call()
使用输出缓冲,如果是这种情况,那么我的函数什么都不输出,它们只返回对象。因此在这种情况下使Zend_Cache_Frontend_Function
无效?
答案 0 :(得分:1)
似乎Zend_Cache_Frontend_Class
是要走的路,而不是Function
。我需要在每次调用之前设置类以确保正在使用正确的对象;
$country = new Default_Db_Table_Country();
$cache->setCachedEntity($country);
$country = $cache->getCountryByName($country_name, $id);
此前端会缓存对象,数组等。