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

Type conversion like Redis rust #152

Open
ymwjbxxq opened this issue Sep 11, 2023 · 2 comments
Open

Type conversion like Redis rust #152

ymwjbxxq opened this issue Sep 11, 2023 · 2 comments

Comments

@ymwjbxxq
Copy link

Hello,

If I use Redis this is the syntax:

let value: Option<String> = cache_client.get(&key).await.ok();
if let Some(value) = value {
   let item = serde_json::from_str::<Item>(&value).unwrap();
   items.lock().unwrap().push(item);
} else {
  warning!("MISSING CACHE KEY: {}", key);
}

Using Momento:

let get_response = cache_client
   .get("cache_name", key)
   .await
   .map_err(|e| error!("ERROR - Momento - get_object {:?}", e))
   .ok();

if let Some(get_response) = get_response {
   let value: Option<String> = match get_response {
      Get::Hit { value } => value.try_into().ok(),
      Get::Miss => None,
   };

   if let Some(value) = value {
      let item = serde_json::from_str::<MyItem>(&value)?;
      items.push(item);
   } else {
      warning!("MISSING CACHE KEY: {}", key);
   }
} else {
   error!("MOMENTO get is null: {}", key);
}

Is there a way to convert types quickly? https://docs.rs/redis/latest/redis/#type-conversions

Thank you so much for your help.

@kvcache
Copy link
Contributor

kvcache commented Sep 11, 2023

Hi @ymwjbxxq, thanks for this issue. We don’t have a way right now to do the succinct type conversion. I agree with you that it would be a better experience.

By the way, I think you can get rid of your intermediate String in the redis path by implementing https://docs.rs/redis/latest/redis/trait.FromRedisValue.html for Item which will let you directly receive Option

@kvcache
Copy link
Contributor

kvcache commented Sep 11, 2023

@cprice404 we should think about whether we would be best served by integrating with serde or using a separate trait (like the redis crate) or both.

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

No branches or pull requests

2 participants