别忘了在php.ini里面
- memcached.use_sasl = 1
复制到/ThinkPHP/Library/Think/Session/Driver 目录下
'MEMCACHE_HOST'=>'1.ocs.aliyuncs.com', //连接地址
'MEMCACHE_PORT'=>'11211', //端口
'MEMCACHE_USERNAME'=>'username', //用户名
'MEMCACHE_PASSWORD'=>'password', //密码
'SESSION_TYPE'=>'Ocs', //名称
<?php
// +----------------------------------------------------------------------// | ThinkPHP [ OCS Session 驱动 ] 3.1.3
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.c2567.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ChenSuiLong <496928838@qq.com>
// +----------------------------------------------------------------------
defined('THINK_PATH') or exit();
/**
* 阿里云OCS方式Session驱动
* @category Extend
* @package Extend
* @subpackage Driver.Session
* @author ChenSuiLong <496928838@qq.com>
*/
class SessionOcs {
/**
* Session有效时间
*/
protected $lifeTime = '';
/**
* Memcached object
*/
protected $Mem = array();
/**
* 打开Session
* @access public
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName) {
$this->lifeTime = C('SESSION_EXPIRE')?C('SESSION_EXPIRE'):ini_get('session.gc_maxlifetime');
$options = array(
'timeout' => C('SESSION_TIMEOUT') ? C('SESSION_TIMEOUT') : 1,
'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : 0
);
$this->Mem = new Memcached();
//dump($this->Mem);exit;
$hosts = explode(',', C('MEMCACHE_HOST'));
$ports = explode(',', C('MEMCACHE_PORT'));
$usernames = explode(',', C('MEMCACHE_USERNAME'));
$passwords = explode(',', C('MEMCACHE_PASSWORD'));
//循环缓存信息,主要是用于集群部署
foreach ($hosts as $i=>$host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$username = isset($usernames[$i]) ? $usernames[$i] : $usernames[0];
$password = isset($passwords[$i]) ? $passwords[$i] : $passwords[0];
$this->Mem->addServer($host, $port);
$this->Mem->setOption(Memcached::OPT_COMPRESSION, false); //关闭压缩功能
$this->Mem->setOption(Memcached::OPT_BINARY_PROTOCOL, true); //使用binary二进制协议
$this->Mem->setSaslAuthData($username,$password);
}
return true;
}
/**
* 关闭Session
* @access public
*/
public function close() {
$this->gc($this->lifeTime);
$this->Mem->close();
$this->Mem = null;
return true;
}
/**
* 读取Session
* @access public
* @param string $sessID
*/
public function read($sessID) {
return $this->Mem->get($this->sessionName.$sessID);
}
/**
* 写入Session
* @access public
* @param string $sessID
* @param String $sessData
*/
public function write($sessID,$sessData) {
return $this->Mem->set($this->sessionName.$sessID, $sessData, 0, $this->lifeTime);
}
/**
* 删除Session
* @access public
* @param string $sessID
*/
public function destroy($sessID) {
return $this->Mem->delete($this->sessionName.$sessID);
}
/**
* Session 垃圾回收
* @access public
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime) {
return true;
}
/**
* 打开Session
* @access public
*/
public function execute() {
session_set_save_handler(array(&$this,"open"),
array(&$this,"close"),
array(&$this,"read"),
array(&$this,"write"),
array(&$this,"destroy"),
array(&$this,"gc"));
}
}