redis使用函数总结

2 篇文章 0 订阅
#include "teb_local_planner/utils/redis_util.hpp"

Redis::Redis() {}

Redis::~Redis() {
    _connect = nullptr;
    _reply = nullptr;
}

bool Redis::connect(const string &host, int port) {
    _connect = redisConnect(host.c_str(), port);
    if (_connect != nullptr && _connect->err) {
        cout << "connect error: " << _connect->errstr << endl;
        return false;
    }

    return true;
}

string Redis::get(const string &key) {
    string str = "";

    _reply = static_cast<redisReply *>(redisCommand(_connect, "GET %s", key.c_str()));
    if (_reply == nullptr) {
        return str;
    }

    str = _reply->str;
    freeReplyObject(_reply);

    return str;
}

void Redis::set(const string &key, const string &value) {
    redisCommand(_connect, "SET %s %s", key.c_str(), value.c_str());
}

void Redis::lpush(const string &key, const string &value) {
    redisCommand(_connect, "LPUSH %s %s", key.c_str(), value.c_str());
}

int Redis::lget(const string &key, int begin_index, int end_index, std::vector<string> *data) {
    _reply = static_cast<redisReply *>(redisCommand(_connect, "LRANGE %s %d %d", key.c_str(), begin_index, end_index));
    if (_reply != nullptr && _reply->type == REDIS_REPLY_ARRAY) {
        data->empty();
        for (int i = 0; i < _reply->elements; ++i) {
            data->push_back(_reply->element[i]->str);
        }
    }

    freeReplyObject(_reply);
    return 1;
}

string Redis::hget(const char *key, const char *hkey) {
    const char *argv[3];
    size_t argvlen[3];
    argv[0] = "HGET";
    argvlen[0] = 4;
    argv[1] = key;
    argvlen[1] = strlen(key);
    argv[2] = hkey;
    argvlen[2] = strlen(hkey);
    _reply = (redisReply *) redisCommandArgv(_connect, 3, argv, argvlen);
    std::string value;
    if (_reply->type != REDIS_REPLY_NIL) {
        value = std::string(_reply->str, _reply->str + _reply->len);
    }

    freeReplyObject(_reply);
    return value;
}

int Redis::hset(const char *key, const char *hkey, const char *hvalue) {
    _reply = static_cast<redisReply *>(redisCommand(_connect, "HSET %s %s %s", key, hkey, hvalue));
    freeReplyObject(_reply);
    return 1;
}

int Redis::hset(const char *key, const char *hkey, const char *hvalue, size_t hvaluelen) {
    const char *argv[4];
    size_t argvlen[4];
    argv[0] = "HSET";
    argvlen[0] = 4;
    argv[1] = key;
    argvlen[1] = strlen(key);
    argv[2] = hkey;
    argvlen[2] = strlen(hkey);
    argv[3] = hvalue;
    argvlen[3] = hvaluelen;
    _reply = (redisReply *) redisCommandArgv(this->_connect, 4, argv, argvlen);
    freeReplyObject(_reply);
    return 1;
}

int Redis::del(const char *key) {
    int res = 0;
    _reply = (redisReply *) redisCommand(this->_connect, "DEL %s", key);
    if (_reply->type == REDIS_REPLY_INTEGER) {
        if (_reply->integer == 1L)
            res = 1;
    }
    freeReplyObject(_reply);
    return res;
}

int Redis::existsKey(const char* key){
    _reply = (redisReply*)redisCommand(this->_connect, "exists %s", key);
    int res = 0;
    if(_reply->type == REDIS_REPLY_INTEGER){
        if(_reply->integer == 1L)
            res  = 1;
    }
    freeReplyObject(_reply);
    return res;
}

//
// Created by auser on 2021/9/11.
//

#ifndef SRC_REDIS_UTIL_HPP
#define SRC_REDIS_UTIL_HPP

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <hiredis/hiredis.h>

using namespace std;

class Redis {
public:
    Redis();

    ~Redis();

    bool connect(const string &host, int port);

    string get(const string &key);

    void set(const string &key, const string &value);

    string hget(const char *key, const char *hkey);

    int hset(const char *key, const char *hkey, const char *hvalue, size_t hvaluelen);

    int hset(const char *key, const char *hkey, const char *hvalue);

    void lpush(const string &key, const string &value);

    int lget(const string &key, int begin_index, int end_index,std::vector<string> *data);

    int existsKey(const char *key);

    int del(const char *key);

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

#endif //SRC_REDIS_UTIL_HPP

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值