Skip to content

Commit a0e4fd5

Browse files
committed
add Select and Ping commands
1 parent b20c325 commit a0e4fd5

File tree

6 files changed

+111
-14
lines changed

6 files changed

+111
-14
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [0.2.1] - 2021-08-20
4+
5+
* cmd: Add Select and Ping commands
6+
37
## [0.2.0] - 2021-06-27
48

59
* upgrade to ntex-0.4

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-redis"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["ntex contributors <[email protected]>"]
55
description = "Redis client"
66
documentation = "https://docs.rs/ntex-redis"
@@ -21,12 +21,12 @@ openssl = ["ntex/openssl"]
2121
rustls = ["ntex/rustls"]
2222

2323
[dependencies]
24-
ntex = "0.4.0-b.1"
24+
ntex = "0.4.0-b.2"
2525
itoa = "0.4.5"
2626
btoi = "0.4.2"
2727
log = "0.4"
2828
derive_more = "0.99"
2929

3030
[dev-dependencies]
3131
rand = "0.8"
32-
env_logger = "0.8"
32+
env_logger = "0.9"

src/cmd/connection.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use ntex::util::ByteString;
2+
3+
use super::{Command, CommandError};
4+
use crate::codec::{Request, Response};
5+
6+
/// SELECT redis command
7+
///
8+
/// Select the Redis logical database having the specified zero-based
9+
/// numeric index.
10+
///
11+
/// ```rust
12+
/// use ntex_redis::{cmd, RedisConnector};
13+
///
14+
/// #[ntex::main]
15+
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
16+
/// let redis = RedisConnector::new("127.0.0.1:6379").connect().await?;
17+
///
18+
/// // select db for current connection
19+
/// let success = redis.exec(cmd::Select(1)).await?;
20+
///
21+
/// assert!(success);
22+
///
23+
/// Ok(())
24+
/// }
25+
/// ```
26+
pub fn Select(db: u32) -> SelectCommand {
27+
SelectCommand(Request::Array(vec![
28+
Request::from_static("SELECT"),
29+
Request::BulkInteger(db as i64),
30+
]))
31+
}
32+
33+
pub struct SelectCommand(Request);
34+
35+
impl Command for SelectCommand {
36+
type Output = bool;
37+
38+
fn to_request(self) -> Request {
39+
self.0
40+
}
41+
42+
fn to_output(val: Response) -> Result<Self::Output, CommandError> {
43+
match val {
44+
Response::String(val) => Ok(val == "OK"),
45+
_ => Ok(false),
46+
}
47+
}
48+
}
49+
50+
/// PING redis command
51+
///
52+
/// This command is often used to test if a connection is still alive,
53+
/// or to measure latency.
54+
///
55+
/// ```rust
56+
/// use ntex_redis::{cmd, RedisConnector};
57+
///
58+
/// #[ntex::main]
59+
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
60+
/// let redis = RedisConnector::new("127.0.0.1:6379").connect().await?;
61+
///
62+
/// // ping connection
63+
/// let response = redis.exec(cmd::Ping()).await?;
64+
///
65+
/// assert_eq!(&response, "PONG");
66+
///
67+
/// Ok(())
68+
/// }
69+
/// ```
70+
pub fn Ping() -> PingCommand {
71+
PingCommand(Request::Array(vec![Request::from_static("PING")]))
72+
}
73+
74+
pub struct PingCommand(Request);
75+
76+
impl Command for PingCommand {
77+
type Output = ByteString;
78+
79+
fn to_request(self) -> Request {
80+
self.0
81+
}
82+
83+
fn to_output(val: Response) -> Result<Self::Output, CommandError> {
84+
match val {
85+
Response::String(val) => Ok(val),
86+
Response::Error(val) => Err(CommandError::Error(val)),
87+
_ => Err(CommandError::Output("Unknown response", val)),
88+
}
89+
}
90+
}

src/cmd/hashes.rs

-10
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,6 @@ impl HSetCommand {
128128
self.0.push(field.into());
129129
self.0.push(value.into());
130130
}
131-
132-
#[doc(hidden)]
133-
#[deprecated(since = "0.1.3", note = "Please use the `entry` function instead")]
134-
/// Insert field to a redis hashmap
135-
pub fn insert<K, V>(self, field: K, value: V) -> Self
136-
where
137-
BulkString: From<K> + From<V>,
138-
{
139-
self.entry(field, value)
140-
}
141131
}
142132

143133
impl Command for HSetCommand {

src/cmd/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use super::codec::{Request, Response};
55
use super::errors::CommandError;
66

77
mod auth;
8+
mod connection;
89
mod hashes;
910
mod keys;
1011
mod lists;
1112
mod strings;
1213
mod utils;
1314

1415
pub use self::auth::Auth;
16+
pub use self::connection::{Ping, Select};
1517
pub use self::hashes::{HDel, HGet, HGetAll, HIncrBy, HLen, HSet};
1618
pub use self::keys::{Del, Exists, Expire, ExpireAt, Ttl, TtlResult};
1719
pub use self::lists::{LIndex, LPop, LPush, RPop, RPush};

tests/test_redis.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async fn test_hashes() {
160160
let key = new_key();
161161

162162
let result = redis
163-
.exec(cmd::HSet(&key, "field1", "1").insert("field2", "2"))
163+
.exec(cmd::HSet(&key, "field1", "1").entry("field2", "2"))
164164
.await
165165
.unwrap();
166166
assert_eq!(result, 2);
@@ -189,3 +189,14 @@ async fn test_hashes() {
189189
let result = redis.exec(cmd::HGetAll(&key)).await.unwrap();
190190
assert!(result.is_empty());
191191
}
192+
193+
#[ntex::test]
194+
async fn test_connection() {
195+
let redis = connect().await;
196+
197+
let result = redis.exec(cmd::Ping()).await.unwrap();
198+
assert_eq!(result, "PONG");
199+
200+
let result = redis.exec(cmd::Select(1)).await.unwrap();
201+
assert!(result);
202+
}

0 commit comments

Comments
 (0)