当前位置:网站首页>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
边栏推荐
- QT learning 26 integrated example of layout management
- 【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
- Leetcode 90: subset II
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- Codeforce c.strange test and acwing
- Linux server development, redis source code storage principle and data model
- Force buckle 144 Preorder traversal of binary tree
- Shell 脚本的替换功能实现
- Pytest+allure+jenkins environment -- completion of pit filling
- Pytorch(六) —— 模型调优tricks
猜你喜欢

【数字IC验证快速入门】11、Verilog TestBench(VTB)入门

探索干货篇!Apifox 建设思路

Cnopendata American Golden Globe Award winning data

2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

Common method signatures and meanings of Iterable, collection and list

【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门

Es FAQ summary
![[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
![[webrtc] m98 Screen and Window Collection](/img/b1/1ca13b6d3fdbf18ff5205ed5584eef.png)
[webrtc] m98 Screen and Window Collection
随机推荐
[webrtc] m98 Screen and Window Collection
padavan手动安装php
Pytorch parameter initialization
Pytest+allure+jenkins environment -- completion of pit filling
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
【p2p】本地抓包
C language queue
Force buckle 145 Binary Tree Postorder Traversal
Button wizard collection learning - mineral medicine collection and running map
JS quick start (I)
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Main window in QT learning 27 application
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Qt学习28 主窗口中的工具栏
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
【webrtc】m98 screen和window采集
Redis technology leak detection and filling (II) - expired deletion strategy
Qt学习27 应用程序中的主窗口
Ansible
[guess-ctf2019] fake compressed packets