当前位置:网站首页>Summary of redis functions
Summary of redis functions
2022-07-07 08:03:00 【Absent mindedness】
#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
边栏推荐
- pytest+allure+jenkins环境--填坑完毕
- 开源生态|打造活力开源社区,共建开源新生态!
- Chip design data download
- 【VHDL 并行语句执行】
- You Li takes you to talk about C language 6 (common keywords)
- 芯片资料 网站 易特创芯
- Codeforces Global Round 19
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- 2022年茶艺师(中级)考试试题及模拟考试
猜你喜欢
mysql多列索引(组合索引)特点和使用场景
Codeforces Global Round 19
padavan手动安装php
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
[2022 actf] Web Topic recurrence
Leetcode 90: subset II
php导出百万数据
开源生态|打造活力开源社区,共建开源新生态!
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
随机推荐
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
pytest+allure+jenkins環境--填坑完畢
Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
Chip information website Yite Chuangxin
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
[UTCTF2020]file header
[CV] Wu Enda machine learning course notes | Chapter 8
Paddlepaddle 29 dynamically modify the network structure without model definition code (relu changes to prelu, conv2d changes to conv3d, 2D semantic segmentation model changes to 3D semantic segmentat
解决问题:Unable to connect to Redis
Pytest+allure+jenkins environment -- completion of pit filling
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
Ansible
MySQL multi column index (composite index) features and usage scenarios
【obs】win-capture需要winrt
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
Linux server development, MySQL cache strategy
Detailed explanation of Kalman filter for motion state estimation
Pytorch(六) —— 模型调优tricks
Thinkcmf6.0 installation tutorial
Ansible