Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
840 views
in Technique[技术] by (71.8m points)

tp5 实例化redis 出错,求解

框架内缓存驱动redis不能满足需要,想要自己连接redis,在助手函数中新增load_redis 函数代码如下

/**
 * lubTicket redis 操作API
 * @param  string $apiport 要操作的接口
 * @param  string $key     键名
 * @param  string $value   键值
 * @param  string $time    有效时间
 * @return true|false  
 */
function load_redis($apiport,$key,$value = '',$time = ''){
    $redis = new Redis();
    $redis->connect(thinkConfig::get('REDIS_HOST'),thinkConfig::get('REDIS_PORT'));
    $redis->select(thinkConfig::get('REDIS_DATABASE'));
    switch ($apiport) {
        case 'lsize':
            //判断列表中元素个数
            $return = $redis->lsize($key);
            break;
        case 'rPop':
            //获取队列中最后一个元素,且移除
            if((int)$redis->lsize($key) > 0){
                $return = $redis->rPop($key);
            }else{
                $return = false;
            }
            break;
        case 'lpush':
            //写入带处理队列,若存在则不再写入
            $return = $redis->lPush($key,$value);
            break;
        case 'set':
            $return = $redis->set($key,$value);
            break;
        case 'setex':
            /**
             * 设置有效期
             */
            $return = $redis->setex($key, $time, $value);
            break;
        case 'get':
            $return = $redis->get($key);
            break;
        case 'lrange':
            //返回list 中的元素 返回名称为key的list中start至end之间的元素(end为 -1 ,返回所有) value 为开始位置 $time 为结束位置
            $return = $redis->lrange($key,$value,$time);
            break;
        case 'delete':
            //删除指定key
            $return = $redis->delete($key);
            break;
    }
    return $return;
}

运行返回如下错误:
Redis::connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
服务器环境正常,redis 运行正常,若使用tp5提供的缓存驱动可以正常使用reids
求大神指点

看到系统缓存驱动内有提示说使用redisd 驱动,请问驱动在那下载


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

先确认是否读到配置文件再说


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...