Initial commit

This commit is contained in:
SunnyQjm committed 2020-05-30 16:51:58 +08:00
commit 2ae7003e27
5 files changed
+218

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
# Project exclude paths
/cmake-build-debug/
+7
View File
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.16)
project(MIRCertificationManager)
set(CMAKE_CXX_STANDARD 14)
add_executable(MIRCertificationManager main.cpp utils/RedisUtil.cpp)
target_link_libraries(MIRCertificationManager hiredis)
+14
View File
@@ -0,0 +1,14 @@
#include <iostream>
#include "utils/RedisUtil.h"
int main() {
RedisUtil redisUtil;
redisUtil.connect("127.0.0.1", 6379);
std::cout << redisUtil.set("test", "1234") << std::endl;
auto result = redisUtil.get("test1");
std::cout << result << std::endl;
std::cout << "exists: " << redisUtil.exist("test") << std::endl;
redisUtil.del("test");
std::cout << "exists: " << redisUtil.exist("test") << std::endl;
return 0;
}
+108
View File
@@ -0,0 +1,108 @@
//
// Created by mingj on 2020/5/30.
//
#include "RedisUtil.h"
#include <iostream>
RedisUtil::RedisUtil() = default;
RedisUtil::~RedisUtil() {
redisFree(this->_connect);
this->_connect = nullptr;
this->_connect = nullptr;
}
/**
* 连接到redis server
* @param host ip
* @param port 端口
* @return
*/
bool RedisUtil::connect(const std::string &host, int port) {
// 连接到指定的redis服务器
this->_connect = redisConnect(host.c_str(), port);
if (this->_connect == nullptr || this->_connect->err) {
std::cerr << "connect error: " << this->_connect->errstr << std::endl;
return false;
}
return true;
}
/**
* 从redis中根据指定的键取到指定的值
* @param key
* @return
* 如果key对应的条目存在,则返回条目对应的值
* 如果key对应的条目不存在,则会返回空字符串(所以如果要判定当前key对应的条目是否存在,则可以调用 RedisUtil::exists )
*/
std::string RedisUtil::get(const std::string &key) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "GET %s", key.c_str()));
std::string str = this->_reply->str;
if (this->_reply->str == nullptr) {
return std::string();
}
freeReplyObject(this->_reply);
return str;
}
/**
* 设置或者更新键值对
* @param key
* @param value
* @return
*/
bool RedisUtil::set(const std::string &key, const std::string &value) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str()));
return std::strcmp(this->_reply->str, "OK") == 0;
}
/**
* 删除某个key对应的条目
* @param key
* @return 返回受影响的条目,比如:返回1表示成功删除一个key对应的条目
*/
int RedisUtil::del(const std::string &key) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "DEL %s", key.c_str()));
return static_cast<int>(this->_reply->integer);
}
/**
* 判断某个key对应的条目是否存在
* @param key
* @return 如果存在则返回大于0的值,典型值为1
*/
int RedisUtil::exist(const std::string &key) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "EXISTS %s", key.c_str()));
return static_cast<int>(this->_reply->integer);
}
/**
* 设置key对应条目的生存期,单位为毫秒;
*
* ps: 1. 如果一个key对应条目的生存期设置成功,则在经过指定的时间之后,该条目会过期,并且从redis中删除
* 2. 默认情况下,没用调用本函数设置条目的生存期的话,条目是持久保存的,在被删除之前会一直存在于redis当中
* @param key
* @return 如果key对应的条目存在且设置生存期成功,则返回一个大于0的值;否则返回0
*/
int RedisUtil::setLifeTime(const std::string &key, long lifetime) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "PEXPIRE %s %ld", key.c_str(), lifetime));
return static_cast<int>(this->_reply->integer);
}
/**
* 获取key对应条目的剩余存活时间,单位为毫秒;
*
* ps: 详情参考redis-cli的PTTL命令 => https://www.runoob.com/redis/keys-pttl.html
*
* @param key
* @return
* -2 => 返回-2表示key对应的条目不存在(可能是本来就没有这个条目,或者本来有一个条目,但是因为设置了存活期,其存活期已到,被移除了)
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* >= 0 => 返回大于等于0的值表示key对应的条目存在,且其剩余的存活时间为返回的值,单位为毫秒
*/
long RedisUtil::getRemainingTime(const std::string &key) {
this->_reply = static_cast<redisReply *>(redisCommand(this->_connect, "PTTL %s", key.c_str()));
return static_cast<int>(this->_reply->integer);
}
+87
View File
@@ -0,0 +1,87 @@
//
// Created by mingj on 2020/5/30.
//
#ifndef MIRCERTIFICATIONMANAGER_REDISUTIL_H
#define MIRCERTIFICATIONMANAGER_REDISUTIL_H
#include <cstring>
#include <string>
#include <hiredis/hiredis.h>
class RedisUtil {
public:
RedisUtil();
virtual ~RedisUtil();
/**
* 连接到redis server
* @param host ip
* @param port 端口
* @return
*/
bool connect(const std::string &host, int port);
/**
* 从redis中根据指定的键取到指定的值
* @param key
* @return
* 如果key对应的条目存在,则返回条目对应的值
* 如果key对应的条目不存在,则会返回空字符串(所以如果要判定当前key对应的条目是否存在,则可以调用 RedisUtil::exists )
*/
std::string get(const std::string &key);
/**
* 设置或者更新键值对
* @param key
* @param value
* @return
*/
bool set(const std::string &key, const std::string &value);
/**
* 删除某个key对应的条目
* @param key
* @return 返回受影响的条目,比如:返回1表示成功删除一个key对应的条目
*/
int del(const std::string &key);
/**
* 判断某个key对应的条目是否存在
* @param key
* @return 如果存在则返回大于0的值,典型值为1
*/
int exist(const std::string &key);
/**
* 设置key对应条目的生存期,单位为毫秒;
*
* ps: 1. 如果一个key对应条目的生存期设置成功,则在经过指定的时间之后,该条目会过期,并且从redis中删除
* 2. 默认情况下,没用调用本函数设置条目的生存期的话,条目是持久保存的,在被删除之前会一直存在于redis当中
* @param key
* @return 如果key对应的条目存在且设置生存期成功,则返回一个大于0的值;否则返回0
*/
int setLifeTime(const std::string &key, long lifetime);
/**
* 获取key对应条目的剩余存活时间,单位为毫秒;
*
* ps: 详情参考redis-cli的PTTL命令 => https://www.runoob.com/redis/keys-pttl.html
*
* @param key
* @return
* -2 => 返回-2表示key对应的条目不存在(可能是本来就没有这个条目,或者本来有一个条目,但是因为设置了存活期,其存活期已到,被移除了)
* -1 => 返回-1表示key对应的条目存在,但是没有设置过存活期,是持久保存的
* >= 0 => 返回大于等于0的值表示key对应的条目存在,且其剩余的存活时间为返回的值,单位为毫秒
*/
long getRemainingTime(const std::string &key);
private:
redisContext *_connect{};
redisReply *_reply{};
};
#endif //MIRCERTIFICATIONMANAGER_REDISUTIL_H