Home > OS >  How to run Redis sadd commands with hiredis
How to run Redis sadd commands with hiredis

Time:12-02

My code contains a head file redis.h and a c source file redis.cpp.

This is a demo of sadd opeaion in redis. All the operations fail, becase of WRONGTYPE Operation against a key holding the wrong kind of value. I don't know what happened.

Please give me some suggestions.

//redis.h
#ifndef _REDIS_H_
#define _REDIS_H_

#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>

#include <hiredis/hiredis.h>

using namespace std;

class Redis{
public:
    Redis(){}
    ~Redis(){
        this->_connect =NULL;
        this->_reply=NULL;
    }

    bool connect(string host, int port){
        this->_connect = redisConnect(host.c_str(), port);
        if(this->_connect != NULL && this->_connect->err){
            printf("connect error: %s\n", this->_connect->errstr);
            return 0;
        }
        return 1;
    }

    string set(string key, string value){
        this->_reply = (redisReply*)redisCommand(this->_connect, "sadd %s %s", key.c_str(), value.c_str());
        string str = this->_reply->str;
        return str;
    }

    string output(string key){
        this->_reply = (redisReply*)redisCommand(this->_connect, "smembers %s", key.c_str());
        string str = this->_reply->str;
        freeReplyObject(this->_reply);
        return str;
    }

private:
    redisContext * _connect;
    redisReply* _reply;
};

#endif //_REDIS_H

//redis.cpp
#include "redis.h"

int main(){
    Redis *r = new Redis();
    if(!r->connect("127.0.0.1", 6379)){
        printf("connect error!\n");
        return 0;
    }
    printf("Sadd names Andy %s\n", r->set("names", "Andy").c_str());
    printf("Sadd names Andy %s\n", r->set("names", "Andy").c_str());
    printf("Sadd names Alice %s\n", r->set("names", "Alice").c_str());
    printf("names members: %s\n", r->output("names").c_str());
    delete r;
    return 0;
}

The result:

Sadd names Andy WRONGTYPE Operation against a key holding the wrong kind of value

Sadd names Andy WRONGTYPE Operation against a key holding the wrong kind of value

Sadd names Alice WRONGTYPE Operation against a key holding the wrong kind of value

names members: WRONGTYPE Operation against a key holding the wrong kind of value

CodePudding user response:

WRONGTYPE Operation against a key holding the wrong kind of value

This means the key, i.e. names, has already been set, and its type is NOT a SET. You can run TYPE names with redis-cli to see the type of the key.

Also, your code has several problems:

  • redisConnect might return null pointer
  • you did not call redisFree to free the resource of redisReply in your set method
  • sadd and smembers do NOT return string reply, so you cannot get the correct reply

Since you're using C , you can try redis-plus-plus, which is based on hiredis, and have more C friendly interface:

try {
    auto r = sw::redis::Redis("tcp://127.0.0.1:6379");
    r.sadd("names", "Andy");
    r.sadd("names", "Alice");
    std::vector<std::string> members;
    r.smembers("names", std::back_inserter(members));
} catch (const sw::redis::Error &e) {
    // error handle
}

Disclaimer: I'm the author of redis-plus-plus.

  • Related