当前位置:网站首页>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
边栏推荐
- Linux server development, SQL statements, indexes, views, stored procedures, triggers
- Qt学习26 布局管理综合实例
- 探索干货篇!Apifox 建设思路
- Rust versus go (which is my preferred language?)
- Regular e-commerce problems part1
- Redis technology leak detection and filling (II) - expired deletion strategy
- dash plotly
- Introduction to basic components of wechat applet
- Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
- Sign up now | oar hacker marathon phase III, waiting for your challenge
猜你喜欢
Who has docker to install MySQL locally?
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
[webrtc] M98 screen and window acquisition
[guess-ctf2019] fake compressed packets
Leetcode 40: combined sum II
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Figure out the working principle of gpt3
QT learning 26 integrated example of layout management
Thinkcmf6.0安装教程
Explore dry goods! Apifox construction ideas
随机推荐
padavan手动安装php
Live broadcast platform source code, foldable menu bar
自定义类加载器加载网络Class
Force buckle 145 Binary Tree Postorder Traversal
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
Button wizard script learning - about tmall grabbing red envelopes
Common validation comments
C language queue
【p2p】本地抓包
芯片资料 网站 易特创芯
Zhilian + AV, AITO asked M7 to do more than ideal one
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
JS quick start (I)
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Linux server development, MySQL cache strategy
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
有 Docker 谁还在自己本地安装 Mysql ?
[SUCTF 2019]Game