# redis-cpp - lightweight C++ client library for Redis
redis-cpp is a C++17 library for executing Redis [commands](https://redis.io/commands) with support for pipelines and the publish / subscribe pattern. Moreover, you can extend the library with your own stream implementation to communicate with Redis. You can also use it like a RESP serializer (pure core). You need only know a couple of functions to start working with Redis.
```cpp
// Connect to server
auto stream = rediscpp::make_stream("localhost", "6379");
// Execute command
std::cout << rediscpp::execute(*stream, "ping").as<std::string>() << std::endl;
```
And you may dive deeper if you feel the need.
**NOTE**
If you need a C++11 version you could switch to c++11 branch and use that one.
# Version
1.1.0
# Features
- easy way to access Redis
- pipelines
- publish / subscribe
- pure core in C++ for the RESP
- extensible transport
- header-only library if it's necessary
- minimal dependencies
- various levels of usage
# License
Distributed under the MIT License
# Compiler and OS
This has compiled and tested within gcc 9.3 and clang 10.0 on Ubuntu 20.04.
You might try other compiler or OS.
**NOTE**
All code is a cross-platform.
# Dependencies
- Boost (at least 1.71 only for using with built-in implementation of transport).
# Build and install
## Build library
```bash
git clone https://github.com/tdv/redis-cpp.git
cd redis-cpp
mkdir build
cd build
cmake ..
make
make install
```
You can use CMAKE_INSTALL_PREFIX to select the installation directory
Moreover, you can use cmake options to configure the library for header-only or pure core.
Instead of cmake options, you can define REDISCPP_HEADER_ONLY and use the library as header-only without any cmake file.
**NOTE**
redis-cpp has two build options
- Pure core only
- Header-only
Use cmake -D with REDISCPP_HEADER_ONLY or REDISCPP_PURE_CORE. You can enable both options at the same time.
You can use your own transport with the 'pure core' option.
If you need to use the header-only library, you can copy the folder redis-cpp from *include/redis-cpp* in your project and define the macro REDISCPP_HEADER_ONLY before including the redis-cpp headers following the example code below:
```cpp
#define REDISCPP_HEADER_ONLY
#include <redis-cpp/stream.h>
#include <redis-cpp/execute.h>
// Include something else
```
## Build examples
```bash
cd examples/{example_project}
mkdir build
cd build
cmake ..
make
```
# Examples
**NOTE**
Look at the redis-docker folder to get all you need to start testing redis-cpp. There are files to build and run a Redis server in Docker.
## Ping
[Source code](https://github.com/tdv/redis-cpp/tree/master/examples/ping)
**Description**
The "Ping" example demonstrates how to execute a Redis command.
```cpp
// STD
#include <cstdlib>
#include <iostream>
#include <redis-cpp/stream.h>
#include <redis-cpp/execute.h>
int main()
{
try
{
auto stream = rediscpp::make_stream("localhost", "6379");
auto response = rediscpp::execute(*stream, "ping");
std::cout << response.as<std::string>() << std::endl;
}
catch (std::exception const &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
```
## Set and Get data
[Source code](https://github.com/tdv/redis-cpp/tree/master/examples/setget)
**Description**
The example demonstrates how to set and get a value.
```cpp
// STD
#include <cstdlib>
#include <iostream>
#include <redis-cpp/stream.h>
#include <redis-cpp/execute.h>
int main()
{
try
{
auto stream = rediscpp::make_stream("localhost", "6379");
auto const key = "my_key";
auto response = rediscpp::execute(*stream, "set",
key, "Some value for 'my_key'", "ex", "60");
std::cout << "Set key '" << key << "': " << response.as<std::string>() << std::endl;
response = rediscpp::execute(*stream, "get", key);
std::cout << "Get key '" << key << "': " << response.as<std::string>() << std::endl;
}
catch (std::exception const &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
```
## Pipeline
[Source code](https://github.com/tdv/redis-cpp/tree/master/examples/pipeline)
**Description**
It's a more complicated example which demonstrates how to use a pipeline within Redis to achieve better performance.
```cpp
// STD
#include <cstdlib>
#include <iostream>
#include <redis-cpp/stream.h>
#include <redis-cpp/execute.h>
int main()
{
try
{
auto stream = rediscpp::make_stream("localhost", "6379");
int const N = 10;
auto const key_pref = "my_key_";
// Executing command 'SET' N times without getting any response
for (int i = 0 ; i < N ; ++i)
{
auto const item = std::to_string(i);
rediscpp::execute_no_flush(*stream,
"set", key_pref + item, item , "ex", "60");
}
// Flush all
std::flush(*stream);
// Getting response for each sent 'SET' request
for (int i = 0 ; i < N ; ++i)
{
rediscpp::value value{*stream};
std::cout << "Set " << key_pref << i << ": "
<< value.as<std::string_view>() << std::endl;
}
// Executing command 'GET' N times without getting any response
for (int i = 0 ; i < N ; ++i)
{
rediscpp::execute_no_flush(*stream, "get",
key_pref + std::to_string(i));
}
// Flush all
std::flush(*stream);
// Getting response for each sent 'GET' request
for (int i = 0 ; i < N ; ++i)
{
rediscpp::value value{*stream};
std::cout << "Get " << key_pref << i << ": "
<< value.as<std::string_view>() << std::endl;
}
}
catch (std::exception const &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
```
## Resp
[Source code](https://github.com/tdv/redis-cpp/tree/master/examples/resp)
**Description**
The "Resp" example demonstrates a basic RESP serialization within redis-cpp without communication with Redis server. It's meant to show you how to use RESP serialization with redis-cpp library.
```cpp
// STD
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <redis-cpp/execute.h>
namespace resps = rediscpp::resp::serialization;
namespace respds = rediscpp::resp::deserialization;
auto make_sample_data()
{
std::ostringstream stream;
put(stream, resps::array{
resps::simple_string{"This is a simple string."},
resps::error_message{"This is an error message."},
resps::bulk_string{"This is a bulk string."},
resps::integer{100500},
resps::array{
resps::simple_string("This is a simple string in a nested array."),
resps::bulk_string("This is a bulk string in a nested array.")
}
});
return stream.str();
}
void print_value(respds::array::item_type const &value, std::ostream &stream)
{
std::visit(rediscpp::resp::detail::overloaded{
[&stream] (respds::simple_string const &val)
{ stream << "Simple string: " << val.get() << std::endl; },
[&stream] (respds::error_message const &val)
{ stream << "Error message: " << val.get() << std::endl; },
[&stream] (respds::bulk_string const &val)
{ stream << "Bulk string: " << val.get() << std::endl; },
[&stream] (respds::integer const &val)
{ stream << "Integer: " << val.get() << std::endl; },
[&stream] (respds::array const &val)
{
stream << "----- Array -----" << std::endl;
for (auto
徐浪老师
- 粉丝: 8527
- 资源: 1万+
最新资源
- 基于游戏内运动矢量的游戏视频编码新方法及其对HEVC编码性能的影响
- 基于高效率视频编码(HEVC)的快速跳过模式决策方法研究-强调编码质量优化
- 信息融合与状态估计 主要是针对多传感器多时滞(包括状态之后和观测滞后)系统,基于改进后的协方差交叉融合(ICI)方法实现对状态的融合估计 对三传感器的时滞系统,利用改进后的协方差交叉融合(ICI)方
- 在本文章中JWT使用的sql
- s32k144 uds bootloader软件,包含上位机 上位机为周立功ZCANPRO脚本,操作简单, 非常适合学习调试
- 具有DMA控制功能的总线接口设计与实现
- Java Swing编写的图书管理系统源码+数据库.zip
- 光伏发电+boost+储能+双向dcdc+并网逆变器控制(低压用户型电能路由器仿真模型) 包含Boost、Buck-boost双向DCDC、并网逆变器三大控制部分 boost电路应用mppt, 采用扰
- 基于合并模式和运动估计的快速编码单元编码方案以优化HEVC视频编码器的计算复杂度
- 相场法水力压裂:流固耦合,裂缝流动与达西流动耦合模型
- 空气流注放电仿真cosmol等离子体模型空气流柱放电
- 个人博客页面设计html + css实现源码.zip
- 这个只是整体的Flac3d隧道台阶法开挖的命令流,送全断面法 但是如果做自己的所需要的内容,肯定是 需要自己写代码(只需要改锚杆命令和钢拱架命令和测点命令)和自己的模型
- 三相PWM整流器仿真,PI双闭环控制,电流可双向流动,四象限运行参考张兴的PWM整流器 波形分别为电网电压、电网电流、直流测电压、电流波形
- 部分失真消除技术在HEVC整数运动估计中绝对差值和架构中的应用与优化研究
- 视频编码优化之快速运动估计算法研究与应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈