|  | 
 
| 本帖最后由 liaow10 于 2016-10-13 16:55 编辑 
 $config['redis']=true;
 $config['socket_type'] = 'tcp';
 $config['host'] = '127.0.0.1';
 $config['password'] = NULL;
 $config['port'] = 6379;
 $config['timeout'] = 0;
 
 上面这串配置是放在config/redis.php里面的,按照官网手册上面的说明新建的一个文件,然后写上这些内容。在我自己的控制器User.php中,原本是使用这种方式来返回客户端消息的:
 
 function response($result, $data) {
 $_SESSION['last_active'] = time();
 $rep = array(
 'result' => $result,
 'data' => $data);
 header("Content-type: application/json");
 echo json_encode($rep);
 exit();
 }
 
 
 在处理函数中直接调用response($res, $data); 加入关于redis cache的有关内容之前,一切正常,返回如下:
 
 {"result":0,"data":{"user":{"id":"3","username":"h@gmail.com","nickname":"heldf"},"token":"xI4U"}}
 
 ----------------------------------------------
 
 下面开始使用redis cache。在response()返回之前,仅仅加了这句,用于测试redis是否配置成功,$this->cache->redis->is_supported(); 然后客户端就得到了这样的返回:
 
 $config['redis']=true;
 $config['socket_type'] = 'tcp';
 $config['host'] = '127.0.0.1';
 $config['password'] = NULL;
 $config['port'] = 6379;
 $config['timeout'] = 0;
 
 {"result":0,"data":{"user":{"id":"3","username":"h@gmail.com","nickname":"heldf"},"token":"xI4U"}}
 
 正常情况应该是只有下面那些内容,而不包含上面那串config的
 
 求各位老司机帮助!!!(下面是详细代码,系统是Debian,确定安装了redis服务,命令行使用redis-cli可以连接并使用redis)
 
 ---------------------------------------------
 ---- config/autoload.php
 // 添加了自动载入driver:
 $autoload['drivers'] = array('cache');
 
 ---- controllers/User.php
 // 自己写的控制器
 function response($result, $data) {
 $_SESSION['last_active'] = time();
 $rep = array(
 'result' => $result,
 'data' => $data);
 header("Content-type: application/json");
 echo json_encode($rep);
 exit();
 }
 
 class User extends CI_Controller {
 
 public function __construct()
 {
 parent::__construct();
 }
 
 public function login()
 {
 $res = RETURN_OK;
 $data = NULL;
 
 $this->cache->redis->is_supported(); ////////// Err
 response($res, $data);
 }
 
 }
 
 上面代码,去掉Err行,返回如下:
 {"result":0,"data":null}
 
 加上Err行,返回如下:
 $config['redis']=true;
 $config['socket_type'] = 'tcp';
 $config['host'] = '127.0.0.1';
 $config['password'] = NULL;
 $config['port'] = 6379;
 $config['timeout'] = 0;
 
 {"result":0,"data":null}
 同时,将is_supported()方法改为save()也是一样的,操作成功,查询redis数据库可以查到,save()函数返回值正常,但是返回给客户端的内容总是有这部分内容。如果使用浏览器输入网址,得到的页面包含以上config文字内容,非常奇怪。
   ---------------------------------
 
 我刚刚重新使用了CI3.1.0源码,修改了以下部分:
 1. autoload.php中自动加载driver那里加入了cache
 2. 新增了文件config/redis.php,内容与之前一致
 3. 修改源码中的Welcome.php,修改后内容如下:
 public function index()
 {
 // $this->cache->redis->is_supported();
 echo "hehe";
 exit();
 //$this->load->view('welcome_message');
 }
 4. 根目录下index.php复制到apache目录,修改正确的system和application地址
 
 然后用浏览器查看对应网址,结果如下:
 hehe
 之后放开redis的注释,再次使用浏览器查看,结果如下:
 $config['redis']=true; $config['socket_type']='tcp'; $config['host']='127.0.0.1'; $config['password']=NULL; $config['port']=6379; $config['timeout']=0; hehe
 
 ----------------------
 怀疑是不是跟redis的配置有关系,CI没有配置成功接通redis,但是又确实成功操作了redis,所以想不太明白。
 ----------------------
 找到原因了,因为redis.php文件没有加<?php来声明文件是php,结果就当纯文本输出了,太马虎了。。。
 
 
 
 
 
 
 
 | 
 |