Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuxwin/7e6b49b6144315cc0232953d1b984f76 to your computer and use it in GitHub Desktop.
Save nuxwin/7e6b49b6144315cc0232953d1b984f76 to your computer and use it in GitHub Desktop.
Zend Framework 2 - Session Memcached Configuration with Failover
<?php
'service_manager' => array(
'factories' => [
'Zend\Session\SessionManager' => 'Zend\Session\Service\SessionManagerFactory',
'Zend\Session\Config\ConfigInterface' => 'Zend\Session\Service\SessionConfigFactory',
],
),
?>
<?php
namespace Application\Service;
use Zend\Cache\Storage\Adapter\MemcachedOptions;
use Zend\Cache\Storage\Adapter\MemcachedResourceManager;
use Zend\Cache\StorageFactory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Session\SaveHandler\Cache;
class MemcachedStorageFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$memcachedResourceManager = new MemcachedResourceManager();
$memcachedResourceManager->addServers('default', $serviceLocator->get('Config')['memcached_session']['servers']);
$memcachedResourceManager->setLibOptions('default', $serviceLocator->get('Config')['memcached_session']['lib_options']);
$memcachedOptions = new MemcachedOptions();
$memcachedOptions->setResourceManager($memcachedResourceManager);
$adapter = StorageFactory::adapterFactory('memcached', $memcachedOptions);
return new Cache($adapter);
}
}
<?php
return [
'session_storage' => [
'type' => 'Zend\Session\Storage\SessionArrayStorage'
],
'session_config' => [
'use_cookies' => true,
'hash_function' => 'sha1'
],
'session_manager' => [
'validators' => array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
],
'memcached_session' => [
'enabled' => true,
'servers' => [
'127.0.0.1:11201',
'127.0.0.1:11202',
'127.0.0.1:12114',
'127.0.0.1:11211',
'127.0.0.1:11212',
'127.0.0.1:11219',
],
'lib_options' => [
\Memcached::OPT_CONNECT_TIMEOUT => 10,
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,
// set it to consistent hashing. If one memcached node is dead, its keys (and only its keys) will be evenly distributed to other nodes.
// This is where the magic is done. This is really different from removing one server in your ->addServers() call.
\Memcached::OPT_DISTRIBUTION => \Memcached::DISTRIBUTION_CONSISTENT,
\Memcached::OPT_SERVER_FAILURE_LIMIT => 5,
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,
\Memcached::OPT_RETRY_TIMEOUT => 1,
]
]
];
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Application;
use Zend\ModuleManager\Listener\ConfigListener;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function init(ModuleManager $moduleManager)
{
$events = $moduleManager->getEventManager();
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));
}
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$sessionManager = $e->getApplication()->getServiceManager()->get('Zend\Session\SessionManager');
$sessionManager->start();
}
public function onMergeConfig($e)
{
$configListener = $e->getConfigListener();
$config = $configListener->getMergedConfig(false);
// inject dependency for memcache factory interface, when memcached is enabled in config
if ($config['memcached_session']['enabled'] == true) {
$config['service_manager']['factories']['Zend\Session\SaveHandler\SaveHandlerInterface'] = 'Application\Service\MemcachedStorageFactory';
}
// Pass the changed configuration back to the listener:
$configListener->setMergedConfig($config);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment