我现在知道MemCache不能像我原先预期的那样工作,但我仍然无法在PHP和Node之间传输用户特定的数据(例如登录成员的id)。
到目前为止我只安装了Memcache,但是如果这是我需要的话,也可以安装Redis,但我需要一些基于Node和Apache上的会话或cookie数据来识别每个登录用户的方法( PHP)登录时的服务器。
我宁愿不使用随后用于引用数据库的通用会话ID,但如果必须,我会这样做。
这就是我在PHP中存储的方式
$memcache = new Memcache;
$memcache->connect('localhost',11211);
$uData = json_encode($_SESSION);
$memcache->set('Session',$uData);
这是我原来的Node文件,由于我误解了Memcache的功能,显然必须对其进行重大更改。
/*****************************************
* 1. Configuration
* 2. Declarations & Definitions
* 3. Memcache & Session Vars
* 4. Server Initiation
* 5. Client Connections
* 5-1. On Client Connect
* 6. Helper Functions
* 7. Utility Functions
*****************************************/
/*****************************************
* 1. Configuration
*****************************************/
// Basic config options
var NodePortNumber = 1337; // t3h 13370rz
var MemCachePortNumber = 11211; // default MemCache port is 11211
var MySQLParams = {
'Host': 'localhost',
'User': 'user',
'Pass': 'pass',
'DB': 'database'
};
/*****************************************
* 2. Declarations & Definitions
*****************************************/
var Express = require('express')();
var HTTP = require('http').Server(Express);
var IO = require('socket.io')(HTTP);
var MySQL = require('mysql');
var Database = MySQL.createConnection({
host : MySQLParams.Host,
user : MySQLParams.User,
password : MySQLParams.Pass,
database : MySQLParams.DB
});
/*****************************************
* 3. Memcache & Session Vars
*****************************************/
var MemCache, Session;
// Define a default value for all Session values so undefined warnings aren't thrown for non-members
Session = [];
Session['MemberID'] = 0;
Session['MemberEmail'] = undefined;
Session['MemberPass'] = undefined;
// Get session vars from Memcache
var Memcached = require('memcached');
MemCache = new Memcached('127.0.0.1:'+MemCachePortNumber); // connect to local memcached
MemCache.get('Session', function(err,result){
Session = JSON.parse(result);
});
/*****************************************
* 4. Server Initiation
*****************************************/
// Initiate the Node server
HTTP.listen(NodePortNumber, function(){
console.log('Server created. Socket listening on Port '+NodePortNumber);
});
/*****************************************
* 5. Client Connections
*****************************************/
var LiveClientCounter = 0; var Members = {};
var IsMember = false;
// Event fired every time a new client connects:
IO.sockets.on('connection', function (client) {
/*****************************************
* 5-1. On Client Connect
*****************************************/
var clientID = client.id;
LiveClientCounter++;
IO.emit('connection count', LiveClientCounter);
console.log('Member '+Session['MemberID'] +' connected');
if ( typeof Session['MemberID'] != typeof undefined && Session['MemberID'] > 0 ) {
IsMember = true;
if ( typeof Members[Session['MemberID']] == typeof undefined )
Members[Session['MemberID']] = [];
Members[Session['MemberID']][clientID] = {
SocketOBJ: client,
ClientID: clientID,
MemberID: Session['MemberID']
};
console.log('Key added for Member #'+Session['MemberID']);
}
});
答案 0 :(得分:1)
$memcache = new Memcache;
$memcache->connect('localhost',11211);
$uData = json_encode($_SESSION);
$memcache->set('Session',$uData);
每个用户都会覆盖会话密钥。
你可以做点什么$memcache = new Memcache;
$memcache->connect('localhost',11211);
$_SESSION['id'] = 'Session_' . phpsessionid();
$uData = json_encode($_SESSION);
$memcache->set($_SESSION['id'], $uData);
然后找到一种方法让节点进程知道$ _SESSION ['id']