目录
一.安装 hiredis
二.接口
三.使用
四.总结
C++ 操作 redis 的库有很多. 咱们使用 redis-plus-plus.
这个库的功能强大, 使用简单.
Github 地址: https://github.com/sewenew/redis-plus-plus
redis-plus-plus 是基于 hiredis 实现的.
hiredis 是一个 C 语言实现的 redis 客户端.
因此需要先安装 hiredis. 直接使用包管理器安装即可.
apt install libhiredis-dev
下载 redis-plus-plus 源码
git clone https://github.com/sewenew/redis-plus-plus.git
编译/安装 redis-plus-plus
使用 cmake 构建
cd redis-plus-plus
mkdir build
cd build
cmake ..
make
sudo make install
构建成功后, 会在 /usr/local/include/ 中多出 sw 目录, 并且内部包含 redis-plus-plus 的一系列头文件.
会在 /usr/local/lib/ 中多出一系列 libredis 库文件.
redis 本身支持很多数据类型的键值对,但是在聊天室项目中只涉及到了字符串键值对的操作,因此这里主要介绍字符串键值对的基础操作。
namespace sw
{
namespace redis
{
struct ConnectionOptions
{
std::string host;
int port = 6379;
std::string path;
std::string user = "default";
std::string password;
int db = 0; // 默认 0 号库
bool keep_alive = false;
}
struct ConnectionPoolOptions
{
std::size_t size = 1; //最大连接数量
}
class Redis
{
// uri e.g 'tcp://127.0.0.1:6379'
explicit Redis(const std::string& uri);
explicit Redis(const ConnectionOptions& connection_opts, const ConnectionPoolOptions& pool_opts = {})
//删除当前库中所有数据
void flushdb(bool async = false);
//删除指定键值对
long long del(const StringView& key);
//判断指定键值对是否存在
long long exists(const StringView& key);
//获取一个 string 键值对
OptionalString get(const StringView& key);
//存放一个 string 键值对,且设置过期时间-毫秒
bool set(const StringView& key, const StringView& val, const std::chrono::milliseconds& ttl =
std::chrono::milliseconds(0), // 0 表示不设置超时
UpdateType type = UpdateType::ALWAYS);
void setex(const StringView& key,
long long ttl,
const StringView& val);
//向一个列表中尾插/头插 string 键值对
long long rpush(const StringView& key, const StringView
& val);
long long lpush(const StringView& key, const StringView
& val);
long long rpush(const StringView& key,Input first, Input last);
// std::vector elements;
// redis.lrange("list", 0, -1,
std::back_inserter(elements));
void lrange(const StringView& key,long long start, long long stop, Output output);
}
}
}
这里只进行字符串键值对的增删改查操作以及数据的生命周期设置。
main.cc:
#include
#include
#include
#include
#include
DEFINE_bool(redis_keep_alive, true, "是否保持长连接");
DEFINE_int32(redis_db, 0, "redis 库号");
DEFINE_int32(redis_port, 6379, "redis 服务器端口");
DEFINE_string(redis_host, "127.0.0.1", "redis 服务器 IP 地址");
std::shared_ptr predis;
void add()
{
predis->set("用户会话 1", "用户 ID1");
predis->set("用户会话 2", "用户 ID2",
std::chrono::milliseconds(1000)); // 设置 1000ms 过期时间
predis->set("用户会话 3", "用户 ID3");
predis->set("用户会话 4", "用户 ID4");
predis->set("用户会话 5", "用户 ID5");
}
void get()
{
auto res1 = predis->get("用户会话 1");
if (res1)
std::cout << *res1 << std::endl;
auto res2 = predis->get("用户会话 2");
if (res2)
std::cout << *res2 << std::endl;
auto res3 = predis->get("用户会话 3");
if (res3)
std::cout << *res3 << std::endl;
auto res4 = predis->get("用户会话 4");
if (res4)
std::cout << *res4 << std::endl;
auto res5 = predis->get("用户会话 5");
if (res5)
std::cout << *res5 << std::endl;
}
void update()
{
predis->set("用户会话 1", "用户 ID 变成 31");
predis->set("用户会话 4", "用户 ID 变成 41",
std::chrono::milliseconds(1000));
predis->del("用户会话 5");
}
void push_test()
{
predis->rpush("群聊会话 1", "成员 1");
predis->rpush("群聊会话 1", "成员 2");
predis->rpush("群聊会话 1", "成员 3");
predis->rpush("群聊会话 1", "成员 4");
predis->rpush("群聊会话 1", "成员 5");
predis->rpush("群聊会话 2", "成员 6");
predis->rpush("群聊会话 2", "成员 7");
predis->rpush("群聊会话 2", "成员 8");
predis->rpush("群聊会话 2", "成员 9");
predis->rpush("群聊会话 2", "成员 0");
std::vector res;
predis->lrange("群聊会话 1", 0, -1, std::back_inserter(res));
for (const auto &r : res)
{
std::cout << r << std::endl;
}
}
int main()
{
sw::redis::ConnectionOptions opts;
opts.host = FLAGS_redis_host;
opts.port = FLAGS_redis_port;
opts.db = FLAGS_redis_db;
opts.keep_alive = FLAGS_redis_keep_alive;
predis = std::make_shared(opts);
std::cout << "--------add-------\n";
add();
std::cout << "--------get-------\n";
get();
std::cout << "--------2s get-------\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
get();
std::cout << "--------update-------\n";
update();
std::cout << "--------2s get-------\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
get();
std::cout << "--------push-------\n";
push_test();
return 0;
}
makefile:
main : main.cc
g++ -std=c++17 $^ -o $@ -lredis++ -lhiredis -lgflags -pthread
运行结果:
本篇文章是专注于我项目中使用到的客户端redis-plus-plus来进行介绍和简单使用,供大家和本人在日常开发中进行参考。