[CakePHP]How to use Session in conjunction with Memcache

    技术2022-06-22  73

    Cakephp default session is saved in the configuration /etc/php.ini,

    this was defined in app/config/core.php:

        Configure::write('Session.save', 'php');

     

    most likely it looks as below:

        session.save_handler = files     session.save_path = "/var/lib/php/session"

     

    this kind of file based session/cache will not work correctly under load balancer/multi web app servers.

    the solution is using database or Memcache.

     

    For using Memcache, we should change the config to:

        Configure::write('Session.save', 'cache');

    and enable the Memcache engine in core.php

     

        Cache::config('default', array(         'engine' => 'Memcache', //[required]         'duration'=> 3600, //[optional]         'probability'=> 100, //[optional]         'prefix' => Inflector::slug(APP_DIR) . '_', //[optional]  prefix every cache file with this string         'servers' => array(             '127.0.0.1:11211' // localhost, default port 11211         ), //[optional]         'compress' => false, // [optional] compress data in Memcache (slower, but uses less memory)     ));

     

    we can check if the memcache has been installed and running by phpinfo() and ps -ef|grep memcache

     

    but unfortunately, even you installed/configured memcache and cakephp well both, this will still not work.

    you will lose sessions when navigating pages.

    if you enable debug mode, you will see errors in the bottom of the page:

    Fatal error: Class 'Debugger' not found in /var/www/cake/libs/debugger.php on line 252 Warning: Invalid callback CakeSession::__close, class 'CakeSession' not found in Unknown on line 0

     

    to solve this problem, you have to apply below patch:

    http://cakephp.lighthouseapp.com/projects/42648/tickets/825-debuggerphp-error-when-session-set-to-cache

     

    --- public_html/cake/libs/model/datasources/datasource.php.orig 2010-11-09 15:56:06.897482341 -0600 +++ public_html/cake/libs/model/datasources/datasource.php 2010-11-09 15:57:10.341327526 -0600 @@ -521,6 +521,12 @@ class DataSource extends Object { $this->rollback($null); } if ($this->connected) { + if ((Configure::read('Session.save') == 'cache' || + Configure::read('Session.save') == 'database') + && function_exists('session_write_close')) { + session_write_close(); + } + $this->close(); } }

    最新回复(0)