# Boost.Redis
Boost.Redis is a high-level [Redis](https://redis.io/) client library built on top of
[Boost.Asio](https://www.boost.org/doc/libs/release/doc/html/boost_asio.html)
that implements the Redis protocol
[RESP3](https://github.com/redis/redis-specifications/blob/master/protocol/RESP3.md).
The requirements for using Boost.Redis are:
* Boost. The library is included in Boost distributions starting with 1.84.
* C++17 or higher.
* Redis 6 or higher (must support RESP3).
* Gcc (10, 11, 12), Clang (11, 13, 14) and Visual Studio (16 2019, 17 2022).
* Have basic-level knowledge about [Redis](https://redis.io/docs/)
and [Boost.Asio](https://www.boost.org/doc/libs/1_82_0/doc/html/boost_asio/overview.html).
The latest release can be downloaded on
https://github.com/boostorg/redis/releases. The library headers can be
found in the `include` subdirectory and a compilation of the source
```cpp
#include <boost/redis/src.hpp>
```
is required. The simplest way to do it is to included this header in
no more than one source file in your applications. To build the
examples and tests cmake is supported, for example
```cpp
# Linux
$ BOOST_ROOT=/opt/boost_1_84_0 cmake --preset g++-11
# Windows
$ cmake -G "Visual Studio 17 2022" -A x64 -B bin64 -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake
```
<a name="connection"></a>
## Connection
Let us start with a simple application that uses a short-lived
connection to send a [ping](https://redis.io/commands/ping/) command
to Redis
```cpp
auto co_main(config const& cfg) -> net::awaitable<void>
{
auto conn = std::make_shared<connection>(co_await net::this_coro::executor);
conn->async_run(cfg, {}, net::consign(net::detached, conn));
// A request containing only a ping command.
request req;
req.push("PING", "Hello world");
// Response where the PONG response will be stored.
response<std::string> resp;
// Executes the request.
co_await conn->async_exec(req, resp, net::deferred);
conn->cancel();
std::cout << "PING: " << std::get<0>(resp).value() << std::endl;
}
```
The roles played by the `async_run` and `async_exec` functions are
* `async_exec`: Execute the commands contained in the
request and store the individual responses in the `resp` object. Can
be called from multiple places in your code concurrently.
* `async_run`: Resolve, connect, ssl-handshake,
resp3-handshake, health-checks, reconnection and coordinate low-level
read and write operations (among other things).
### Server pushes
Redis servers can also send a variety of pushes to the client, some of
them are
* [Pubsub](https://redis.io/docs/manual/pubsub/)
* [Keyspace notification](https://redis.io/docs/manual/keyspace-notifications/)
* [Client-side caching](https://redis.io/docs/manual/client-side-caching/)
The connection class supports server pushes by means of the
`boost::redis::connection::async_receive` function, which can be
called in the same connection that is being used to execute commands.
The coroutine below shows how to used it
```cpp
auto
receiver(std::shared_ptr<connection> conn) -> net::awaitable<void>
{
request req;
req.push("SUBSCRIBE", "channel");
generic_response resp;
conn->set_receive_response(resp);
// Loop while reconnection is enabled
while (conn->will_reconnect()) {
// Reconnect to channels.
co_await conn->async_exec(req, ignore, net::deferred);
// Loop reading Redis pushes.
for (;;) {
error_code ec;
co_await conn->async_receive(resp, net::redirect_error(net::use_awaitable, ec));
if (ec)
break; // Connection lost, break so we can reconnect to channels.
// Use the response resp in some way and then clear it.
...
consume_one(resp);
}
}
}
```
<a name="requests"></a>
## Requests
Redis requests are composed of one or more commands (in the
Redis documentation they are called
[pipelines](https://redis.io/topics/pipelining)). For example
```cpp
// Some example containers.
std::list<std::string> list {...};
std::map<std::string, mystruct> map { ...};
// The request can contain multiple commands.
request req;
// Command with variable length of arguments.
req.push("SET", "key", "some value", "EX", "2");
// Pushes a list.
req.push_range("SUBSCRIBE", list);
// Same as above but as an iterator range.
req.push_range("SUBSCRIBE", std::cbegin(list), std::cend(list));
// Pushes a map.
req.push_range("HSET", "key", map);
```
Sending a request to Redis is performed with `boost::redis::connection::async_exec` as already stated.
### Config flags
The `boost::redis::request::config` object inside the request dictates how the
`boost::redis::connection` should handle the request in some important situations. The
reader is advised to read it carefully.
<a name="responses"></a>
## Responses
Boost.Redis uses the following strategy to support Redis responses
* `boost::redis::request` is used for requests whose number of commands are not dynamic.
* **Dynamic**: Otherwise use `boost::redis::generic_response`.
For example, the request below has three commands
```cpp
request req;
req.push("PING");
req.push("INCR", "key");
req.push("QUIT");
```
and its response also has three comamnds and can be read in the
following response object
```cpp
response<std::string, int, std::string>
```
The response behaves as a tuple and must
have as many elements as the request has commands (exceptions below).
It is also necessary that each tuple element is capable of storing the
response to the command it refers to, otherwise an error will occur.
To ignore responses to individual commands in the request use the tag
`boost::redis::ignore_t`, for example
```cpp
// Ignore the second and last responses.
response<std::string, boost::redis::ignore_t, std::string, boost::redis::ignore_t>
```
The following table provides the resp3-types returned by some Redis
commands
Command | RESP3 type | Documentation
---------|-------------------------------------|--------------
lpush | Number | https://redis.io/commands/lpush
lrange | Array | https://redis.io/commands/lrange
set | Simple-string, null or blob-string | https://redis.io/commands/set
get | Blob-string | https://redis.io/commands/get
smembers | Set | https://redis.io/commands/smembers
hgetall | Map | https://redis.io/commands/hgetall
To map these RESP3 types into a C++ data structure use the table below
RESP3 type | Possible C++ type | Type
---------------|--------------------------------------------------------------|------------------
Simple-string | `std::string` | Simple
Simple-error | `std::string` | Simple
Blob-string | `std::string`, `std::vector` | Simple
Blob-error | `std::string`, `std::vector` | Simple
Number | `long long`, `int`, `std::size_t`, `std::string` | Simple
Double | `double`, `std::string` | Simple
Null | `std::optional<T>` | Simple
Array | `std::vector`, `std::list`, `std::array`, `std::deque` | Aggregate
Map | `std::vector`, `std::map`, `std::unordered_map` | Aggregate
Set | `std::vector`, `std::set`, `std::unordered_set` | Aggregate
Push | `std::vector`, `std::map`, `std::unordered_map` | Aggregate
For example, the response to the request
```cpp
request req;
req.push("HELLO", 3);
req.push_range("RPUSH", "key1", vec);
req.push_range("HSET", "key2", map);
req.push("LRANGE", "key3", 0, -1);
req.push("HGETALL", "key4");
req.push("QU
关于boost-1-86-0.tar.gz,这是Boost C++ Libraries的一个压缩包版本,包含了Boost库的所有
需积分: 0 124 浏览量
更新于2024-08-21
收藏 141.88MB GZ 举报
关于boost_1_86_0.tar.gz,这是Boost C++ Libraries的一个压缩包版本,包含了Boost库的所有源代码和必要的文件,用于在Linux、Windows等操作系统上进行编译和安装。
Seal^_^
- 粉丝: 3w+
- 资源: 40
最新资源
- 风光储VSG并网,储能为锂电池 0.6s引入预同步算法,实现稳定并网 工况多,波形好
- 同步磁阻电机SynRM无传感器高频注入HFI+mras驱动matlab离散模型,包含文献,用于学习研究
- 基于粒子群算法的光伏MPPT(可重启PSO) 光伏最大功率追踪算法模型simulink MPPT是基于粒子群算法实现的,同时具备动态追踪能力,当光照改变后会重启粒子群算法进行最大功率追踪
- Comsol等离子体仿真,Ar棒板流注放电 电子密度,电子温度,三维视图,电场强度等
- 储能参与调峰调频联合调度模型(matlab代码) 主要内容为考虑储能同时参与调峰以及调频的联合调度模型,现有文章往往仅关注储能在调峰方面的能力,而实际上同时参与调峰调频将超线性的提高储能的收益,在建模
- Matlab simulink仿真模型搭建(电池相关) 本店可接锂电池或电池包建模搭建 单体电池方面: 1、电池等效电路模型搭建(RC模型) 2、电池特征参数辨识(离线、在线、自适应) 3、电池SOC
- 三相并网逆变器双闭环控制,电网电流外环电容电流内环控制算法,matlab Simulink仿真模型,有源阻尼,单位功率因数,电网电压和电流同相位
- 脉振高频电压注入的永磁同步电机无速度传感器 PMSM
- 三相电压型PWM pwm整流器仿真,双闭环pi PI控制(电压外环电流内环),输出电压600V,单位1运行,变负载实验
- 基于下垂控制的三相全桥PWM逆变器并网仿真模型 基于Matlab Simulink仿真平台 主电路采用三相全桥PWM逆变器 1.仿真均能正常运行,能够准确跟踪对应参考值 2.直流母线电压设置为700V
- 基于扩展反电动势法的PMSM中高速无感控制仿真,对凸极和非凸极电机都适用,模型全部采用离散化的仿真方式,仿照数字控制器真实的特性,有PI+PLL和PI+Luenberger两个版本,龙伯格观测器角度估
- 两极式单相光伏并网仿真 前极:Boost电路+电导增量法 后极:桥式逆变+L型滤波+电压外环电流内环控制 并网电流和电网电压同频同相,单位功率因数并网,谐波失真率0.39%,并网效率高
- 国标GBT34658-2017直流快充27930一致性测试详细报告 对测试用例进行了详细测试,含有通过的BMS快充报文内容 注:同时增加了对测试用例分析和软件兼容性做法
- Comsol等离子体仿真,空气棒板电晕放电 电场强度等
- STM32三相电压型SVPWM整流器仿真,以电压外环和电流内环控制,双闭环PID控制,输出电压600V 三相电压型SVPWM整流器仿真,以电压外环和电流内环控制,双闭环PID控制,输出电压600V
- 电机maxwell Simplorer耦合模型,Maxwell 中建立BLDC电机本体有限元模型,Simplorer中搭建的SVPWM策略下Id=0双闭环控制外电路模型 可成功实现场路耦合联合仿真