我正在尝试创建一个新的(已配置且可以使用)Zend\Cache\Storage\Adapter\Memcached
并收到错误:
文件:[project] /vendor/zendframework/zendframework/library/Zend/Cache/Storage/Adapter/MemcachedResourceManager.php:52 消息:没有id为'default'的资源
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 消息:选项“server”没有匹配的setServer setter方法,必须定义
答案 0 :(得分:0)
正如您已经指出的那样,您必须定义资源 - 表示基本\Memcached
实例或其中一个选项自动实例化资源。 (请查看Zend\Cache\Storage\Adapter\MemcachedResource
)
...原因是在不同的存储适配器实例上共享一个或多个\Memcached
资源。
请注意,AbstractAdapter
不是类型提示的标记 - 您应该通过StorageInterface键入提示;)