使用和配置 Zend Session 和 Zend Cache Memcached - Zend Framework 2.3

Using and Configuring Zend Session and Zend Cache Memcached - Zend Framework 2.3

实际上,我正在使用 "standard" 会话管理器配置: http://framework.zend.com/manual/current/en/modules/zend.session.manager.html

我想使用缓存并将我的会话数据保存到服务器的缓存 (memcached) 中以提高性能和可伸缩性。

我这样设置 php.ini (localhost memcached):

session.save_handler=memcached
session.save_path= "tcp://127.0.0.1" 

并显示此错误:

Warning: session_start(): Cannot find save handler 'memcached' - session startup failed in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\SessionManager.php on line 98

所以,我不明白如何配置我的 config/autoload/global.phpmodule/application/module.php。这是我第一次想总体上实现 memcached 和缓存。非常感谢!

我试过这样修改module/application/module.php

---add session and cache ---

use Zend\Session\Config\SessionConfig;
use Zend\Session\Container;
use Zend\Cache\StorageFactory;
use Zend\Session\SaveHandler\Cache;
use Zend\Session\SessionManager;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Session\Validator\RemoteAddr;

--- end session and cache ---

    public function onBootstrap($e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $this->initSession(array(
    'remember_me_seconds' => 180,
    'use_cookies' => true,
    'cookie_httponly' => true,
    'validators' => array(
    'Zend\Session\Validator\RemoteAddr',
    'Zend\Session\Validator\HttpUserAgent',
    'phpSaveHandler' => 'memcached',
    'savePath' => 'tcp://127.0.0.1',
  )
));
    }

public function initSession($config)
{
  $sessionConfig = new SessionConfig();
  $sessionConfig->setOptions($config);
  $sessionManager = new SessionManager($sessionConfig);
  $sessionManager->getValidatorChain()
          ->attach(
          'session.validate',
            array(new HttpUserAgent(), 'isValid')
          )
          ->attach(
            'session.validate',
            array(new RemoteAddr(), 'isValid')
          );

  $cache = StorageFactory::factory(array(
    'adapter' => array(
    'name' => 'memcached',
    'options' => array(
    'server' => '127.0.0.1',
  ),
)
));

$saveHandler = new Cache($cache);
$sessionManager->setSaveHandler($saveHandler);
$sessionManager->start();

Container::setDefaultManager($sessionManager);

}

但它显示此错误:

    Warning: ini_set() expects parameter 2 to be string, array given in C:\Program Files (x86)\xampp\htdocs\Zend-application\vendor\zendframework\zendframework\library\Zend\Session\Config\SessionConfig.php on line 88

Fatal error: Call to undefined method Zend\Stdlib\CallbackHandler::attach() in C:\Program Files (x86)\xampp\htdocs\Zend-application\module\Application\Module.php on line 68

这是我的config/autoload/global.php

    return array(
   'db' => array(
      'driver'  => 'Pdo_Mysql',
      'charset' => 'utf-8',
      'dsn'            => 'mysql:dbname=mydb;host=localhost',
      'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),

   ),
   'service_manager' => array(
      'factories' => array(
         'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',

      ),
   ),
   'session' => array(
        'config' => array(
            'class' => 'Zend\Session\Config\SessionConfig',
            'options' => array(
                'name' => 'zend-application',
            ),
        ),
        'storage' => 'Zend\Session\Storage\SessionArrayStorage',
        'validators' => array(
            'Zend\Session\Validator\RemoteAddr',
            'Zend\Session\Validator\HttpUserAgent',
        ),
    ),
);

希望它能帮助别人,我解决了我的问题。我在 Win7 环境中工作,但 memcached 无法正常工作!我改变了:

    session.save_handler=memcached
    session.save_path= "tcp://127.0.0.1" 

    session.save_handler=memcache
    session.save_path= "tcp://127.0.0.1:11211" 

我恢复了 "standard" 会话管理器配置,内存缓存正常工作。当我将整个站点传输到 apache 服务器时,我将更改 php.ini 以使用 memcached。

http://framework.zend.com/manual/current/en/modules/zend.session.manager.html