Last active
January 27, 2021 18:59
-
-
Save dkemper/b7527ee73340d525fa29 to your computer and use it in GitHub Desktop.
Zend Framework 2 - Session Memcached Configuration with Failover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
'service_manager' => array( | |
'factories' => [ | |
'Zend\Session\SessionManager' => 'Zend\Session\Service\SessionManagerFactory', | |
'Zend\Session\Config\ConfigInterface' => 'Zend\Session\Service\SessionConfigFactory', | |
], | |
), | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
] | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Sehr pornös! Danke 😃