当前位置: 首页 > news >正文

redis-plus-plus安装与使用

目录

一.安装 hiredis

二.接口

三.使用

 四.总结


C++ 操作 redis 的库有很多. 咱们使用 redis-plus-plus.

这个库的功能强大, 使用简单.

Github 地址: https://github.com/sewenew/redis-plus-plus

一.安装 hiredis

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<std::string> 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 <sw/redis++/redis.h>
#include <iostream>
#include <string>
#include <thread>
#include <gflags/gflags.h>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<sw::redis::Redis> 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<std::string> 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<sw::redis::Redis>(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.ccg++ -std=c++17 $^ -o $@ -lredis++ -lhiredis -lgflags -pthread

运行结果:

 四.总结

        本篇文章是专注于我项目中使用到的客户端redis-plus-plus来进行介绍和简单使用,供大家和本人在日常开发中进行参考。

http://www.dtcms.com/a/289132.html

相关文章:

  • [BUG]关于UE5.6编译时出现“Microsoft.MakeFile.Targets(44,5): Error MSB3073”问题的解决
  • 30天打牢数模基础-SVM讲解
  • Facebook 开源多季节性时间序列数据预测工具:Prophet 快速入门 Quick Start
  • UE5多人MOBA+GAS 26、为角色添加每秒回血回蓝(番外:添加到UI上)
  • Go并发聊天室:从零构建实战
  • Mysql(事务)
  • 30个常用的Linux命令汇总和实战场景示例
  • 30天打牢数模基础-粒子群算法讲解
  • 详解Mysql索引合并
  • Jetpack - ViewModel、LiveData、DataBinding(数据绑定、双向数据绑定)
  • langchain调用本地ollama语言模型和嵌入模型
  • 梯度提升之原理
  • COGNEX康耐视IS5403-01智能相机加Navitar 18R00 LR1010WM52镜头
  • React 英语打地鼠游戏——一个寓教于乐的英语学习游戏
  • [Windows] Bili视频转图文笔记 v1.7.5
  • 网鼎杯2020青龙组notes复现
  • 7. 命令模式
  • Modbus Slave 使用教程:快速搭建模拟从站进行测试与开发
  • Ribbon轮询实现原理
  • Unity笔记——Unity 封装方法指南
  • day24——Java高级技术深度解析:单元测试、反射、注解与动态代理
  • [Python] -项目实战类3- 用Python制作一个记事本应用
  • CVE-2022-41128
  • Python数据处理库与语法总结
  • API获取及调用(以豆包为例实现图像分析)
  • FreeRTOS任务创建与删除
  • 掌握配置文件(三):运用Profile实现多环境配置隔离
  • 三级知识点汇总(详解)【c++】——3
  • 让不符合要求的任何电脑升级Windows11
  • 《通信原理》学习笔记——第五章