4

我正在尝试创建一个新的(已配置并可以使用)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 设置方法

4

1 回答 1

0

正如您已经指出的,您必须定义一个资源 - 意味着一个基本\Memcached实例或自动实例化资源的选项之一。(请看一下Zend\Cache\Storage\Adapter\MemcachedResource)......这样做的原因是\Memcached在不同的存储适配器实例上共享一个或多个资源。

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

于 2013-04-08T07:42:28.907 回答