Getting Zend Framework 2 Memcached Working

I am trying to create a new (configured and ready to use) Zend\Cache\Storage\Adapter\Memcached and get an error message:

File: [project] /vendor/zendframework/zendframework/library/Zend/Cache/Storage/Adapter/MemcachedResourceManager.php:52 Message: No resource with identifier 'default'

Module class

 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

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

Storage class

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

How to make it work?

Thank you in advance!

EDIT:

I ended up MemcachedOptions with setting up servers (see above). Now it works!

Note: servers , not server , because in the Zend\Cache\Storage\Adapter\MemcachedOptions class there is no setServer() method, but only setServers() :

File: /var/www/itt/unisportr-zf/vendor/zendframework/zendframework/library/Zend/Stdlib/AbstractOptions.php: 85 Message: the server option does not have a corresponding setServer setter method that must be defined

+4
source share
1 answer

As you have already noted, you need to define a resource - it means the base instance \Memcached or one of the parameters to auto-accumulate the resource. (Please take a look at Zend\Cache\Storage\Adapter\MemcachedResource ) ... The reason for this is the separation of one or more \Memcached resources into different instances of the storage adapter.

Also note that the AbstractAdapter not a marker for type hints - you must enter a hint using StorageInterface;)

0
source

All Articles