我正在尝试创建一个新的(已配置并可以使用)Zend\Cache\Storage\Adapter\Memcached
并出现错误:
文件:[项目]/vendor/zendframework/zendframework/library/Zend/Cache/Storage/Adapter/MemcachedResourceManager.php:52 消息:没有 ID 为“默认”的资源
Module
班级
class Module implements ConfigProviderInterface, ServiceProviderInterface, AutoloaderProviderInterface {
...
public function getServiceConfig() {
try {
return array (
'factories' => array(
...
'Cache\Adapter\Memcached' => function ($serviceManager) {
$memcached = new Memcached($serviceManager->get('Cache\Adapter\MemcachedOptions'));
return $memcached;
},
'Cache\Adapter\MemcachedOptions' => function ($serviceManager) {
return new MemcachedOptions(array(
'ttl' => 60 * 60 * 24 * 7, // 1 week
'namespace' => 'cache_listener',
'key_pattern' => null,
'readable' => true,
'writable' => true,
'servers' => 'localhost',
));
},
'Search\Model\CityStorage' => function ($serviceManager) {
return new CityStorage(
$serviceManager->get('Search\Model\CityTable'),
$serviceManager->get('Cache\Adapter\Memcached')
);
}
)
);
}
...
}
...
}
Controller
班级
class SearchController extends AbstractActionController {
...
public function searchCoursesAction() {
...
return new ViewModel(array(
...
'cities' => $this->getServiceLocator()->get('Search\Model\CityStorage')->getCities(),
));
}
...
}
Storage
班级
<?php
namespace Search\Model;
use Zend\Cache\Storage\Adapter\AbstractAdapter;
use Search\Model\CityTable;
class CityStorage {
/**
* @var CityTable
*/
private $cityTable;
/**
* @var AbstractAdapter
*/
private $cacheAdapter;
public function __construct(CityTable $cityTable, AbstractAdapter $cacheAdapter) {
$this->cityTable = $cityTable;
$this->cacheAdapter = $cacheAdapter;
$this->cacheCities();
}
public function getCities() {
if (!$this->cacheAdapter->hasItem('cities')) {
$this->cacheCities();
}
return $cacheAdapter->getItem('cities');
}
private function cacheCities() {
// $cities = $this->cityTable->fetchAll()->toArray();
// $this->cacheAdapter->setItem('cities', $cities);
$this->cacheAdapter->setItem('test', 123);
}
}
如何让它工作?
先感谢您!
编辑:
我已经完成了MemcachedOptions
设置servers
(见上文)。现在它起作用了!
注意:servers
而不是server
,因为没有方法setServer()
,而只有setServers()
在Zend\Cache\Storage\Adapter\MemcachedOptions
类中:
文件:/var/www/itt/unisportr-zf/vendor/zendframework/zendframework/library/Zend/Stdlib/AbstractOptions.php:85 消息:选项“服务器”没有必须定义的匹配 setServer 设置方法