rust通过resp连接redis并读取数据

rust通过resp连接redis并读取数据

项目从tcp连接开始,到数据的添加和读取。
原文链接:https://blog.csdn.net/weixin_44259356/article/details/103700811
参考大佬的链接:https://blog.csdn.net/readlnh/article/details/90173980
在大佬的基础上添加了对Hash数据的添加和读取,以及对list数据的添加和读取
主要修改如下:

解析返回数据

fn parse_io(response: &str) -> Option<RedisResult> {                                                                                                                        
    let vec: Vec<&str> = response.split("\r\n").collect();
    match &vec[0][0..1] {
        "$" => return Some(RedisResult::RString(vec[1].to_string())),
        "*" => {
            let mut len = vec[0][1..].parse::<usize>().unwrap();
            let mut v: Vec<String> = Vec::new();
            len =len*2;
            for i in 0..len {
                if(i%2!=0)
                {
                v.push(vec[i + 1].to_string());
                }
            }
            println!("长度为{}",len);
            println!("第一个值为{}",v[0]);
            return Some(RedisResult::RArr(v));
        }
        "+" => return Some(RedisResult::RString(vec[1].to_string())),
        "-" => panic!(vec[0].to_string()),
        _ => return None,
    }
}

项目已托管gitHub
https://github.com/MyRong12138/Rust_Redis

发布了46 篇原创文章 · 获赞 6 · 访问量 9396

猜你喜欢

转载自blog.csdn.net/weixin_44259356/article/details/103700811