cakephp,[CakePHP]How to use Session in conjunction with Memcache

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 _disibledevent=> + Configure::read('Session.save') == 'database')
+ && function_exists('session_write_close')) {
+ session_write_close();
+ }
+
$this->close();
}
}
Tags:  cakephp

延伸阅读

最新评论

发表评论