Skip to content

Commit

Permalink
fixup! editoast: bulk get cached projection
Browse files Browse the repository at this point in the history
  • Loading branch information
flomonster committed Jul 9, 2024
1 parent 5097ac1 commit d14e715
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions editoast/src/redis_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::client::RedisConfig;
use crate::error::Result;
use futures::future;
use futures::FutureExt;
use opentelemetry::Key;
use redis::aio::{ConnectionLike, ConnectionManager};
use redis::cluster::ClusterClient;
use redis::cluster_async::ClusterConnection;
Expand Down Expand Up @@ -83,6 +84,10 @@ impl RedisConnection {
&mut self,
keys: &[K],
) -> Result<Vec<Option<T>>> {
// Avoid mget to fail if keys is empty
if keys.is_empty() {
return Ok(vec![]);
}
let values: Vec<Option<String>> = self.mget(keys).await?;
values
.into_iter()
Expand Down Expand Up @@ -145,24 +150,26 @@ impl RedisConnection {
&mut self,
items: &[(K, T)],
) -> Result<()> {
// Avoid mset to fail if keys is empty
if items.is_empty() {
return Ok(());
}
let mut ser_items = vec![];
for (key, value) in items.iter() {
let str_value = match serde_json::to_string(value) {
Ok(value) => value,
Err(_) => {
return Err(RedisError::from((
ErrorKind::IoError,
"An error occured serializing to json",
))
.into())
}
};
ser_items.push((key, str_value));
}
self.mset(&ser_items).await?;
let serialized_items = items
.iter()
.map(|(key, value)| {
serde_json::to_string(value)
.map(|str_value| (key, str_value))
.map_err(|_| {
RedisError::from((
ErrorKind::IoError,
"An error occured serializing to json",
))
.into()
})
})
.collect::<Result<Vec<_>>>()?;

self.mset(&serialized_items).await?;
Ok(())
}
}
Expand Down

0 comments on commit d14e715

Please sign in to comment.