Update..
parent
460855f0ef
commit
d25c4e47e9
|
@ -1,6 +1,6 @@
|
|||
Drogon 支持 Redis,Redis是一种非常快速的内存数据存储。 可以用作数据库缓存或消息代理。 与 Drogon 中其他原建一樣,Redis的操作是异步的。 这确保了 Drogon 即使在重负载下也能以非常高的并发性运行。
|
||||
Drogon 支持 Redis,Redis是一种非常快速的内存数据存储。 可以用作数据库缓存或消息代理。 与 Drogon 中其他组件一樣,Redis的操作是异步的。 这确保了 Drogon 即使在重负载下也能以非常高的并发性运行。
|
||||
|
||||
Redis 支持依赖于hiredis 库。 如果在构建 Drgon 时hiredis 不可用,则Redis支持将不可用。
|
||||
Redis 支持依赖于hiredis 库。 如果在构建 Drogon 时hiredis 不可用,则Redis支持将不可用。
|
||||
|
||||
### 创建客户端
|
||||
|
||||
|
@ -13,6 +13,31 @@ app().createRedisClient("127.0.0.1", 6379);
|
|||
RedisClientPtr redisClient = app().getRedisClient();
|
||||
```
|
||||
|
||||
另外,与Database客户端一样,Redis客户端也支持config文件配置,也支持配置成Fast模式,具体配置如下:
|
||||
|
||||
```json
|
||||
"redis_clients": [
|
||||
{
|
||||
//name: 客户端名字, 默认值是'default'
|
||||
//"name":"",
|
||||
//host: 服务端IP, 默认值是127.0.0.1
|
||||
"host": "127.0.0.1",
|
||||
//port: 服务端端口号, 默认值是6379
|
||||
"port": 6379,
|
||||
//passwd: 密码,默认为空
|
||||
"passwd": "",
|
||||
//db index: 默认值是0
|
||||
"db": 0,
|
||||
//is_fast: 默认值是false, 是否是fast模式,如果为true,会以更高效的方式运行,但是只能在IO线程或主线程中使用, 并且不能使用同步接口。
|
||||
"is_fast": false,
|
||||
//number_of_connections: 连接数, 默认值是1, 如果is_fast为true, 该数字表示每个IO线程或主线程内的连接数, 否则表示该客户端所有连接数
|
||||
"number_of_connections": 1,
|
||||
//timeout: 超时值,默认值是-1.0, 单位是秒,表示一条命令的超时时间,超过这个时间未得到结果将返回超时错误,0或者负值表示没有超时限制
|
||||
"timeout": -1.0
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
### 使用Redis
|
||||
|
||||
execCommandAsync 以异步方式执行 Redis 命令。 它至少需要3个参数,第一个和第二个是在Redis命令成功或失败时调用的回调。 第三是命令本身。 该命令可以是C风格的格式字符串。 其余部分是格式字符串的参数。 例如,要设置 `name` 為 `drogon`:
|
||||
|
@ -55,7 +80,7 @@ redisClient->execCommandAsync(
|
|||
|
||||
### Redis 事务
|
||||
|
||||
Redis 事务允许在一个步骤中执行多个命令。 事务中的所有命令都按顺序执行,其他客户端的命令不会在事务**中间**执行。 并且事务是原子操作。 这意味着要么执行所有操作,要么出现问题并且在执行事务之前回滚所有内容。
|
||||
Redis 事务允许在一个步骤中执行多个命令。 事务中的所有命令都按顺序执行,其他客户端的命令不会在事务**中间**执行。 注意redis的事务不是原子操作,也就是说收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余的命令依然被执行。redis事务没有回滚操作。
|
||||
|
||||
newTransactionAsync 方法创建一个新事务。 然后就可以像普通的 RedisClient 一样使用事务。 最后,RedisTransaction::execute 方法执行事务。
|
||||
|
||||
|
@ -71,3 +96,7 @@ redisClient->newTransactionAsync([](const RedisTransactionPtr &transPtr) {
|
|||
[](const std::exception &err) { /* transaction failed */ });
|
||||
});
|
||||
```
|
||||
|
||||
### 协程
|
||||
|
||||
Redis 客户端也支持协程. 需要gcc11或者更新的编译器,并且使用`cmake -DCMAKE_CXX_FLAGS=-fcoroutines` 来使能它.
|
||||
|
|
|
@ -13,9 +13,35 @@ app().createRedisClient("127.0.0.1", 6379);
|
|||
RedisClientPtr redisClient = app().getRedisClient();
|
||||
```
|
||||
|
||||
Redis clients can also be created via the configuration file.
|
||||
|
||||
```json
|
||||
"redis_clients": [
|
||||
{
|
||||
//name: Name of the client,'default' by default
|
||||
//"name":"",
|
||||
//host: Server IP, 127.0.0.1 by default
|
||||
"host": "127.0.0.1",
|
||||
//port: Server port, 6379 by default
|
||||
"port": 6379,
|
||||
//passwd: '' by default
|
||||
"passwd": "",
|
||||
//db index: 0 by default
|
||||
"db": 0,
|
||||
//is_fast: false by default, if it is true, the client is faster but user can't call any synchronous interface of it and can't use it outside of the IO threads and the main thread.
|
||||
"is_fast": false,
|
||||
//number_of_connections: 1 by default, if the 'is_fast' is true, the number is the number of connections per IO thread, otherwise it is the total number of all connections.
|
||||
"number_of_connections": 1,
|
||||
//timeout: -1.0 by default, in seconds, the timeout for executing a command.
|
||||
//zero or negative value means no timeout.
|
||||
"timeout": -1.0
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
### Using Redis
|
||||
|
||||
execCommandAsync executes Redis commands in an asynchronous mannar. It takes at least 3 parameters, the first and second are callback whom will be called when the Redis command succeed or failed. The thrid being the command it self. The command could be a C-style format string. And the rests are arguments for the format string. For example, to set the key `name` to `drogon`:
|
||||
execCommandAsync executes Redis commands in an asynchronous manner. It takes at least 3 parameters, the first and second are callback whom will be called when the Redis command succeed or failed. The third being the command it self. The command could be a C-style format string. And the rests are arguments for the format string. For example, to set the key `name` to `drogon`:
|
||||
|
||||
```c++
|
||||
redisClient->execCommandAsync(
|
||||
|
@ -55,7 +81,7 @@ redisClient->execCommandAsync(
|
|||
|
||||
### Transaction
|
||||
|
||||
Redis transaction allows multiple commands to be executed in a single step. All commands within a transaction are executed in order, no commands by other clients can be executed **in middle** of a transaction. And a transaction is atomic. Meaning either everything is executed, or something went wrong and everything is rolled back before executing the transaction.
|
||||
Redis transaction allows multiple commands to be executed in a single step. All commands within a transaction are executed in order, no commands by other clients can be executed **in middle** of a transaction. Note that a transaction is not atomic. This means that after receiving the exec command, the transaction will be executed, If any command in the transaction fails to execute, the rest of the commands will still be executed. redis transactions do not support rollback operations.
|
||||
|
||||
The newTransactionAsync method creates a new transaction. Then the transaction could be used just like a normal RedisClient. Finally, the RedisTransaction::execute method executes said transaction.
|
||||
|
||||
|
@ -71,3 +97,7 @@ redisClient->newTransactionAsync([](const RedisTransactionPtr &transPtr) {
|
|||
[](const std::exception &err) { /* transaction failed */ });
|
||||
});
|
||||
```
|
||||
|
||||
### Coroutines
|
||||
|
||||
Redis clients support coroutines. One should use the gcc11 compiler or a newer compiler and use `cmake -DCMAKE_CXX_FLAGS=-fcoroutines` to enable it.
|
Loading…
Reference in New Issue