fix caching issue
This commit is contained in:
parent
f1561590a8
commit
ac0f6292ed
2 changed files with 25 additions and 6 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use redis::{Client, Commands, RedisResult};
|
||||
use redis::{Client, Commands, ErrorKind, RedisResult};
|
||||
|
||||
use crate::config::RedisConfig;
|
||||
use crate::user_data::UserData;
|
||||
|
|
@ -25,11 +25,29 @@ impl AvatarCache {
|
|||
pub fn get_cache_by_name(&self, name: &str) -> RedisResult<Option<UserData>> {
|
||||
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)),
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
Ok(None)
|
||||
if e.kind() == ErrorKind::TypeError {
|
||||
// No cache for id
|
||||
Ok(None)
|
||||
} else {
|
||||
// Something else went wrong
|
||||
Err(e)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,8 +26,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||
}
|
||||
None => {
|
||||
println!("Cache miss for {}", target);
|
||||
let u = fetch.get_user_by_name(target).await?;
|
||||
dbg!(u);
|
||||
if let Some(u) = fetch.get_user_by_name(target).await? {
|
||||
cache.set_cache_data(&u)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue