New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for Vec<(std::string::String, u16)> and some other small change #1320
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
备注:如果引入 paste 库代码还可以更加简单一点 |
有了这个pull request,现在对redis封装的代码已经挺好看的了 https://github.com/user-tax-dev/redis/blob/main/src/lib.rs mod r#macro;
use fred::{
interfaces::{ClientLike, KeysInterface},
prelude::{ReconnectPolicy, RedisClient, RedisConfig, ServerConfig},
types::RedisKey,
};
use napi::{
bindgen_prelude::{External, FromNapiValue, Uint8Array},
Either,
};
use napi_derive::napi;
use napi_sys::{napi_env, napi_value};
pub type StringUint8Array = Either<String, Uint8Array>;
pub struct Bin(StringUint8Array);
impl FromNapiValue for Bin {
unsafe fn from_napi_value(env: napi_env, napi_val: napi_value) -> napi::Result<Self> {
Ok(Bin(StringUint8Array::from_napi_value(env, napi_val)?))
}
}
impl From<Bin> for RedisKey {
fn from(t: Bin) -> RedisKey {
RedisKey::from(t.0.as_ref())
}
}
#[napi]
pub struct Redis {
c: RedisClient,
}
napiImpl!(
Redis :
get(&self, key: Bin) -> String {
self.c.get::<String, _>(key).await?
}
get_b(&self, key: Bin) -> Uint8Array {
self.c.get::<Vec<u8>, _>(key).await?.into()
}
);
#[napi]
pub fn server_cluster(hosts: Vec<(String, u16)>) -> External<ServerConfig> {
ServerConfig::Clustered { hosts }.into()
}
#[napi]
pub fn server_host_port(host: String, port: u16) -> External<ServerConfig> {
ServerConfig::Centralized { host, port }.into()
}
def!(redis_conn(
version: u8,
server:External<ServerConfig>,
username:Option<String>,
password:Option<String>,
db:Option<u8>
) -> Redis {
let server = server.as_ref().clone();
let mut config = RedisConfig::default();
if version == 3 {
config.version = fred::types::RespVersion::RESP3;
}
config.server = server;
config.database = db;
config.password = password;
config.username = username;
// configure exponential backoff when reconnecting, starting at 100 ms, and doubling each time up to 30 sec.
let policy = ReconnectPolicy::new_exponential(0, 100, 30_000, 2);
let client = RedisClient::new(config);
let _ = client.connect(Some(policy));
let _ = client.wait_for_connect().await?;
Redis { c: client }
}); |
usrtax
changed the title
add support for Vec<(std::string::String, u16)>
add support for Vec<(std::string::String, u16)> and some other small change
Sep 17, 2022
…rs#1319 ) impl Send for External auto AsRef for Either impl<T: 'static> From<T> for External<T>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
add support for Vec<(std::string::String, u16)> and so on ( fix #1319 )
impl Send for External
auto AsRef for Either
impl<T: 'static> From<T> for External<T>