sessioncachingzend-framework2zend-sessionzend-cache

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


Actually, I'm using "standard" sessions manager config: http://framework.zend.com/manual/current/en/modules/zend.session.manager.html

I want to use cache and save my session's data into server's cache (memcached) for improves performances and scalability.

I set php.ini like this (localhost memcached):

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

and it show this error:

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

So, I don't understand how to configure my config/autoload/global.php and module/application/module.php. it's my first time that I want to implement memcached and caching in general. thanks, so much!

I tried to modify module/application/module.php like this:

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

}

but it shows this error:

    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

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

Solution

  • Hoping it'll help someone, I resolved my issue. I'm working in Win7 enviroment and memcached doesn't work on it! I changed :

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

    to

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

    I restored the "standard" session manager config and memcache works correctly. When I'll transfer the entire site to apache server, I'll change php.ini for using memcached.

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