Getting Zend Framework 2 Memcached working



我正在尝试创建一个新的(已配置并准备使用)ZendCacheStorageAdapterMemcached并得到一个错误:

文件:[项目]/供应商/zendframework/zendframework/图书馆/Zend/缓存/存储/适配器/MemcachedResourceManager.php: 52消息:没有id为default的资源

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

class SearchController extends AbstractActionController {
    ...
    public function searchCoursesAction() {
        ...
        return new ViewModel(array(
            ...
            'cities' => $this->getServiceLocator()->get('SearchModelCityStorage')->getCities(),
        ));
    }
    ...
}

Storage class

<?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);
    }
}

如何让它工作?

提前感谢!

编辑:

我已经用设置servers完成了MemcachedOptions(见上文)。现在它工作了!

注意:servers而不是server,因为在ZendCacheStorageAdapterMemcachedOptions类中没有setServer()方法,只有setServers()方法:

文件:/var/www/itt/unisportr-zf/供应商/zendframework/zendframework/图书馆/Zend/Stdlib/AbstractOptions.php: 85消息:选项"server"没有匹配的setServer setter方法,该方法必须定义

您已经注意到,您必须定义一个资源—意味着一个基本的Memcached实例或自动实例化资源的选项之一。(请看ZendCacheStorageAdapterMemcachedResource)…这样做的原因是在不同的存储适配器实例上共享一个或多个Memcached资源。

还请注意,AbstractAdapter不是类型提示的标记-您应该通过StorageInterface输入提示;)

相关内容

  • 没有找到相关文章

最新更新