tp5配置调用redis的方法

1、\thinkphp\library\think\cache\driver 找到 redis.php,打开并且根据自己情况修改里面的配置

  protected $options = [
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '12345678',
        'select'     => 0,
        'timeout'    => 0,
        'expire'     => 0,
        'persistent' => false,
        'prefix'     => '',
    ];

2、\application\config.php,打开并且修改cache驱动方式

'cache'                  => [
        // 驱动方式
        'type'   => 'redis',
        // 缓存保存目录
        'path'   => CACHE_PATH,
        // 缓存前缀
        'prefix' => '',
        // 缓存有效期 0表示永久缓存
        'expire' => 0,
    ],

3、\thinkphp\library\think,找到并打开Cache.php新增一个getHandler方法

public static function getHandler(){
        self::init();
        return self::$handler;
}

4、配置已经配置完了,最后就是如何使用?在你的控制器里调用即可

        $redis = new redis();
	//$redis = Cache::getHandler();//redis资源对象,这里只是tp5封装的redis对象
	//echo "<pre>";
	//print_r($redis);//打印的是redis资源对象
	//$redis->handler();//这样才能获取到原生的redis对象
	$redis->handler()->RPUSH('hello','i miss youR');
	//$redis->set('hello','i am ok');
	print_r( $redis->handler()->LRANGE ('hello',0,10));

现在就可以像上面我写的那样调用redis的任何方法了,拿走不谢!!


版权声明:本文为shuyukui原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。