fix caching issue

This commit is contained in:
Tom Bloor 2024-04-21 14:45:32 +01:00
parent f1561590a8
commit ac0f6292ed
Signed by: TBSliver
GPG key ID: 4657C7EBE42CC5CC
2 changed files with 25 additions and 6 deletions

View file

@ -1,4 +1,4 @@
use redis::{Client, Commands, RedisResult}; use redis::{Client, Commands, ErrorKind, RedisResult};
use crate::config::RedisConfig; use crate::config::RedisConfig;
use crate::user_data::UserData; use crate::user_data::UserData;
@ -25,11 +25,29 @@ impl AvatarCache {
pub fn get_cache_by_name(&self, name: &str) -> RedisResult<Option<UserData>> { pub fn get_cache_by_name(&self, name: &str) -> RedisResult<Option<UserData>> {
let mut con = self.redis.get_connection()?; let mut con = self.redis.get_connection()?;
match con.hgetall(UserData::redis_id_from_str(name)) { let id: String = match con.get(UserData::redis_name_from_str(name)) {
Ok(v) => v,
Err(e) => {
return if e.kind() == ErrorKind::TypeError {
// No cache for name
Ok(None)
} else {
// Something else went wrong
Err(e)
}
}
};
match con.hgetall(UserData::redis_id_from_str(&id)) {
Ok(v) => Ok(Some(v)), Ok(v) => Ok(Some(v)),
Err(e) => { Err(e) => {
dbg!(e); if e.kind() == ErrorKind::TypeError {
// No cache for id
Ok(None) Ok(None)
} else {
// Something else went wrong
Err(e)
}
}, },
} }
} }

View file

@ -26,8 +26,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
} }
None => { None => {
println!("Cache miss for {}", target); println!("Cache miss for {}", target);
let u = fetch.get_user_by_name(target).await?; if let Some(u) = fetch.get_user_by_name(target).await? {
dbg!(u); cache.set_cache_data(&u)?;
}
} }
} }