Skip to content

Type conversion like Redis rust #152

@ymwjbxxq

Description

@ymwjbxxq

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions