2015-02-09 152 views
1

事實上,我使用的「標準」會話管理器配置: http://framework.zend.com/manual/current/en/modules/zend.session.manager.html使用和配置Zend的會話和Zend緩存的Memcached - Zend框架2.3

我想用緩存和我的會話的數據保存到服務器的高速緩存( memcached)來提高性能和可伸縮性。

我這樣設置(本地主機的memcached)的php.ini:

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', 
     ), 
    ), 
); 

回答

1

希望能對大家有所幫助別人,我解決了我的問題。我正在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" 

我恢復了 「標準」 會話管理器配置和memcache正常工作。當我將整個站點轉移到apache服務器時,我將更改使用memcached的php.ini。

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