From 2ae7003e27ca4e3590bb5955fa3c9e7f040eb430 Mon Sep 17 00:00:00 2001 From: SunnyQjm Date: Sat, 30 May 2020 16:51:58 +0800 Subject: [PATCH] Initial commit --- .gitignore | 2 + CMakeLists.txt | 7 +++ main.cpp | 14 ++++++ utils/RedisUtil.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++ utils/RedisUtil.h | 87 +++++++++++++++++++++++++++++++++++ 5 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 utils/RedisUtil.cpp create mode 100644 utils/RedisUtil.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da6ed1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Project exclude paths +/cmake-build-debug/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d61e16a --- /dev/null +++ b/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0c62dbd --- /dev/null +++ b/main.cpp @@ -0,0 +1,14 @@ +#include +#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; +} diff --git a/utils/RedisUtil.cpp b/utils/RedisUtil.cpp new file mode 100644 index 0000000..1da3d27 --- /dev/null +++ b/utils/RedisUtil.cpp @@ -0,0 +1,108 @@ +// +// Created by mingj on 2020/5/30. +// + +#include "RedisUtil.h" +#include + +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(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(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(redisCommand(this->_connect, "DEL %s", key.c_str())); + return static_cast(this->_reply->integer); +} + + +/** + * 判断某个key对应的条目是否存在 + * @param key + * @return 如果存在则返回大于0的值,典型值为1 + */ +int RedisUtil::exist(const std::string &key) { + this->_reply = static_cast(redisCommand(this->_connect, "EXISTS %s", key.c_str())); + return static_cast(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(redisCommand(this->_connect, "PEXPIRE %s %ld", key.c_str(), lifetime)); + return static_cast(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(redisCommand(this->_connect, "PTTL %s", key.c_str())); + return static_cast(this->_reply->integer); +} \ No newline at end of file diff --git a/utils/RedisUtil.h b/utils/RedisUtil.h new file mode 100644 index 0000000..dd9344a --- /dev/null +++ b/utils/RedisUtil.h @@ -0,0 +1,87 @@ +// +// Created by mingj on 2020/5/30. +// + +#ifndef MIRCERTIFICATIONMANAGER_REDISUTIL_H +#define MIRCERTIFICATIONMANAGER_REDISUTIL_H + +#include +#include +#include + +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