(转 +自己总结)thinkphp3.2集成redis

1、配置

//redis
	'DATA_CACHE_PREFIX' => 'Redis_',//缓存前缀
	'DATA_CACHE_TYPE'=>'Redis',//默认动态缓存为Redis
	'REDIS_RW_SEPARATE' => true, //Redis读写分离 true 开启
	'REDIS_HOST'=>'xxx.xx.xx.xx', //redis服务器ip,多台用逗号隔开;读写分离开启时,第一台负责写,其它[随机]负责读;
	'REDIS_PORT'=>'6379',//端口号
	'REDIS_TIMEOUT'=>'300',//超时时间
	'REDIS_PERSISTENT'=>false,//是否长连接 false=短连接
	'REDIS_AUTH'=>'xxx',//AUTH认证密码

2、如果redis服务端有认证密码,则需要改Redis.class.php文件44行下,加

  

if(C('REDIS_AUTH')!=null){
            $this->handler->auth(C('REDIS_AUTH'));
        }

3、使用

<?php
namespace Home\Controller;
use Think\Controller;
use Think\Cache\Driver\Redis;

class TestController extends Controller {

	public function testRedis()
	{


		$redis = new Redis();



		$options = array();
		$options['host'] = C('REDIS_HOST'); // ip  xxx.xxx.xxx.xxx 
		$options['port'] = C('REDIS_PORT'); // 端口号 6379

		$redis->connect('Redis',$options);
		$redis->set('test2','hello world2!');
		echo $redis->get("test2");
	}

}

猜你喜欢

转载自blog.csdn.net/A9925/article/details/81779905