Skip to content
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
merged 2 commits into from Sep 20, 2022

Conversation

usrtax
Copy link
Contributor

@usrtax usrtax commented Sep 17, 2022

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>

@usrtax
Copy link
Contributor Author

usrtax commented Sep 17, 2022

备注:如果引入 paste 库代码还可以更加简单一点

@usrtax
Copy link
Contributor Author

usrtax commented Sep 17, 2022

有了这个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 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>
@Brooooooklyn Brooooooklyn merged commit e54c37a into napi-rs:main Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add support for Vec<(std::string::String, u16)>
2 participants