自学内容网 自学内容网

Redis——redispluspls库hash及zset类型相关接口使用

hash类型相关接口

hset和hget

 std::cout<<"hset 和 hget"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key",std::make_pair("f2","222"));
    //hset能够一次支持多个value键值对
    redis.hset("key",{
        std::make_pair("f3","333"),
        std::make_pair("f4","444")
    });
    vector<std::pair<string,string>> fields = {
        std::make_pair("f5","555"),
        std::make_pair("f6","666")
    };
    redis.hset("key",fields.begin(),fields.end());

    auto result = redis.hget("key","f1");
    if(result) std::cout<<"result:"<<result.value()<<std::endl;

在这里插入图片描述

hexists

std::cout<<"hexist"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");
    bool result = redis.hexists("key","f1");
    std::cout<<"result:"<<result<<std::endl;

在这里插入图片描述

hdel

 std::cout<<"hexist"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");
    bool result = redis.hexists("key","f1");
    std::cout<<"result:"<<result<<std::endl;

在这里插入图片描述

hkeys 和 hvals

std::cout<<"hkeys和hvalues"<<std::endl;
    redis.flushall();

    redis.hset("key","f1","111");
    redis.hset("key","f2","222");
    redis.hset("key","f3","333");
    
    vector<string> fields;
    auto it = std::back_inserter(fields);
    redis.hkeys("key",it);
    
    for(const auto&x:fields){
        std::cout<<x<<std::endl;
    }
    vector<string> values;
    it = std::back_inserter(values);
    redis.hvals("key",it);
     for(const auto&x:values){
        std::cout<<x<<std::endl;
    }

在这里插入图片描述

hmset和hmget

 std::cout<<"hmget和hmset"<<std::endl;
    redis.flushall();

    redis.hmset("key",{
        std::make_pair("f1","111"),
        std::make_pair("f2","222"),
        std::make_pair("f3","333")
    });
    
    std::vector<std::pair<string,string>> pairs = {
        std::make_pair("f4","444"),
        std::make_pair("f5","555"),
        std::make_pair("f6","666"),
    };
    redis.hmset("key",pairs.begin(),pairs.end());
    vector<string> values;
    auto it  = std::back_inserter(values);
    redis.hmget("key",{"f1","f2","f3"},it);
    for(const auto&x:values){
        std::cout<<x<<std::endl;
    }

在这里插入图片描述

zset类型相关接口

zadd和zrange

std::cout<<"zadd和zrange"<<std::endl;
    redis.flushall();

    redis.zadd("key","吕布",99);
    redis.zadd("key",{
        std::make_pair("赵云",98),
        std::make_pair("典韦",97)
    });
    vector<std::pair<string,double>> members = {
        std::make_pair("关羽",96),
        std::make_pair("张飞",95),
    };
    redis.zadd("key",members.begin(),members.end());

    
    //zrange支持两种主要的风格:
    //1. 只查询member,不带score
    //2. 查询member ,同时带score
    //关键就是看插入迭代器指向的容器的类型
    //指向的容器只是包含一个string,就是只查询member
    //指向的容器包含的是一个pair,里面又string和double,就是查询member同时带有分数
    vector<string> membersResults;
    auto it = std::back_inserter(membersResults);
    redis.zrange("key",0,-1,it);
    for(const auto&x: membersResults){
        std::cout<<x<<std::endl;
    }
    vector<std::pair<string,double>> membersWithscore;
    auto it2 = std::back_inserter(membersWithscore);
    redis.zrange("key",0,-1,it2);
    for(const auto&x: membersWithscore){
        std::cout<<x.first<<":"<<x.second<<std::endl;
    }

在这里插入图片描述

zcard 和 zrem

 std::cout<<"zcard 和 zrem"<<std::endl;
    redis.flushall();

    redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);

    long long result = redis.zcard("key");
    std::cout<<"result:"<<result<<std::endl;

    redis.zrem("key","zhangsan");
    result = redis.zcard("key");
    std::cout<<"result:"<<result<<std::endl;

在这里插入图片描述

zscore和zrank

std::cout<<"zscore 和 zrank"<<std::endl;
    redis.flushall();

     redis.zadd("key","zhangsan",90);
    redis.zadd("key","lisi",91);
    redis.zadd("key","wangwu",92);
    redis.zadd("key","zhaoliu",93);
    auto score = redis.zscore("key","zhangsan");
    if(score) std::cout<<"score:"<<score.value()<<std::endl;

    auto rank = redis.zrank("key","lisi");
    if(rank) std::cout<<"rank:"<<rank.value()<<std::endl;

在这里插入图片描述


原文地址:https://blog.csdn.net/m0_70289867/article/details/142424345

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!