获取Zend框架2 Memcached的工作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取Zend框架2 Memcached的工作相关的知识,希望对你有一定的参考价值。
我试图创建一个新的(配置并准备使用)ZendCacheStorageAdapterMemcached
并得到一个错误:
文件:[项目] /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(
...
'CacheAdapterMemcached' => function ($serviceManager) {
$memcached = new Memcached($serviceManager->get('CacheAdapterMemcachedOptions'));
return $memcached;
},
'CacheAdapterMemcachedOptions' => 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',
));
},
'SearchModelCityStorage' => function ($serviceManager) {
return new CityStorage(
$serviceManager->get('SearchModelCityTable'),
$serviceManager->get('CacheAdapterMemcached')
);
}
)
);
}
...
}
...
}
Controller
类
class SearchController extends AbstractActionController {
...
public function searchCoursesAction() {
...
return new ViewModel(array(
...
'cities' => $this->getServiceLocator()->get('SearchModelCityStorage')->getCities(),
));
}
...
}
Storage
类
<?php
namespace SearchModel;
use ZendCacheStorageAdapterAbstractAdapter;
use SearchModelCityTable;
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()
在ZendCacheStorageAdapterMemcachedOptions
类:
文件:/var/www/itt/unisportr-zf/vendor/zendframework/zendframework/library/Zend/Stdlib/AbstractOptions.php:85消息:选项“服务器”不具有必须定义一个匹配setServer setter方法
正如你已经注意到,你必须定义一个资源 - 指的是基本Memcached
实例或选项自动实例化一个资源之一。 (请看看到ZendCacheStorageAdapterMemcachedResource
)......这样做的原因是在不同的存储适配器实例共享一个或多个Memcached
资源。
还请注意,AbstractAdapter
不是类型提示标记 - 你应该由StorageInterface键入提示;)
以上是关于获取Zend框架2 Memcached的工作的主要内容,如果未能解决你的问题,请参考以下文章