[CakePHP]How to use Session in conjunction with Memcache

本文介绍了CakePHP中会话管理的配置方法,特别是在负载均衡或多Web服务器环境下如何正确配置会话存储,包括使用数据库和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 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();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值