protected function registerMemcacheHandler(Application $app)
{
$app['session.handler_factory.backing_memcache'] = $app->protect(function ($connections) {
$memcache = new \Memcache();
foreach ($connections as $conn) {
$args = [$conn['host'] ?: 'localhost', $conn['port'] ?: 11211, $conn['persistent'] ?: false];
if ($conn['weight'] > 0) {
$args[] = $conn['weight'];
if ($conn['timeout'] > 1) {
$args[] = $conn['timeout'];
}
}
call_user_func_array([$memcache, 'addServer'], $args);
if ($conn['weight'] <= 0 && $conn['timeout'] > 1) {
$memcache->setServerParams($args[0], $args[1], $conn['timeout']);
}
}
return $memcache;
});
$app['session.handler_factory.backing_memcached'] = $app->protect(function ($connections) {
$memcache = new \Memcached();
foreach ($connections as $conn) {
$memcache->addServer($conn['host'] ?: 'localhost', $conn['port'] ?: 11211, $conn['weight'] ?: 0);
}
return $memcache;
});
$app['session.handler_factory.memcache'] = $app->protect(function ($options, $key = 'memcache') use($app) {
$connections = $this->parseConnections($options, 'localhost', 11211);
$memcache = $app['session.handler_factory.backing_' . $key]($connections);
$handlerOptions = [];
if (isset($options['expiretime'])) {
$handlerOptions['expiretime'] = $options['expiretime'];
}
if (isset($options['prefix'])) {
$handlerOptions['prefix'] = $options['prefix'];
}
if ($key === 'memcache') {
return new MemcacheHandler($memcache, $handlerOptions);
} else {
return new MemcachedHandler($memcache, $handlerOptions);
}
});
$app['session.handler_factory.memcached'] = $app->protect(function ($options) use($app) {
return $app['session.handler_factory.memcache']($options, 'memcached');
});
}