今天集成OCS照着网上的文章遇到很多大坑,下面介绍一下 正确流程
1.框架公共配置文件 ApplicationCommonConfconfig.php 添加以下配置
'DATA_CACHE_TYPE' => 'Memcache', // 数据缓存类型
'DATA_CACHE_TIMEOUT' => 86400, //缓存超时时间
'MEMCACHE_HOST'=>'....ocs.aliyuncs.com', //阿里云ocs远程地址
'MEMCACHE_PORT'=>'11211', //端口
'MEMCACHE_USERNAME'=>'.....', //用户名
'MEMCACHE_PASSWORD'=>'.....', //密码
'SESSION_TYPE'=>'Memcache', //Session驱动
2.将下面的类放入 ThinkPHPLibraryThinkCacheDriver 目录 文件名称为 Memcache.class.php
C('MEMCACHE_HOST') ? : '127.0.0.1', 'port' => C('MEMCACHE_PORT') ? : 11211, 'timeout' => C('DATA_CACHE_TIMEOUT') ? : false, 'persistent' => false, ), $options); $this->options = $options; $this->options['expire'] = isset($options['expire'])? $options['expire'] : C('DATA_CACHE_TIME'); $this->options['prefix'] = isset($options['prefix'])? $options['prefix'] : C('DATA_CACHE_PREFIX'); $this->options['length'] = isset($options['length'])? $options['length'] : 0; $func = $options['persistent'] ? 'pconnect' : 'connect'; $this->handler = new Memcache; $options['timeout'] === false ? $this->handler->$func($options['host'], $options['port']) : $this->handler->$func($options['host'], $options['port'], $options['timeout']); } /** * 读取缓存 * @access public * @param string $name 缓存变量名 * @return mixed */ public function get($name) { N('cache_read', 1); return $this->handler->get($this->options['prefix'].$name); } /** * 写入缓存 * @access public * @param string $name 缓存变量名 * @param mixed $value 存储数据 * @param integer $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) { N('cache_write', 1); if (is_null($expire)) { $expire = $this->options['expire']; } $name = $this->options['prefix'].$name; if ($this->handler->set($name, $value, 0, $expire)) { if ($this->options['length'] > 0) { // 记录缓存队列 $this->queue($name); } return true; } return false; } /** * 删除缓存 * @access public * @param string $name 缓存变量名 * @return boolean */ public function rm($name, $ttl = false) { $name = $this->options['prefix'].$name; return $ttl === false ? $this->handler->delete($name) : $this->handler->delete($name, $ttl); } /** * 清除缓存 * @access public * @return boolean */ public function clear() { return $this->handler->flush(); } }
3.将下面的类放入 ThinkPHPLibraryThinkSessionDriver 目录 文件名称为 Memcache.class.php
lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime; $options = array( 'timeout' => C('SESSION_TIMEOUT') ? C('SESSION_TIMEOUT') : 1, 'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : 0 ); $this->handle = new Memcache; $hosts = explode(',', C('MEMCACHE_HOST')); $ports = explode(',', C('MEMCACHE_PORT')); foreach ($hosts as $i=>$host) { $port = isset($ports[$i]) ? $ports[$i] : $ports[0]; $this->handle->addServer($host, $port, true, 1, $options['timeout']); } return true; } /** * 关闭Session * @access public */ public function close() { $this->gc(ini_get('session.gc_maxlifetime')); $this->handle->close(); $this->handle = null; return true; } /** * 读取Session * @access public * @param string $sessID */ public function read($sessID) { return $this->handle->get($this->sessionName.$sessID); } /** * 写入Session * @access public * @param string $sessID * @param String $sessData */ public function write($sessID, $sessData) { return $this->handle->set($this->sessionName.$sessID, $sessData, 0, $this->lifeTime); } /** * 删除Session * @access public * @param string $sessID */ public function destroy($sessID) { return $this->handle->delete($this->sessionName.$sessID); } /** * Session 垃圾回收 * @access public * @param string $sessMaxLifeTime */ public function gc($sessMaxLifeTime) { return true; } }
OK。就这么简单 Cache 与 Session 都集成完毕。。祝各位小伙伴一次成功蛤。。
微信扫码关注 layui 公众号