Skip to content

Commit

Permalink
feat: log more cache initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed May 25, 2024
1 parent a1709a1 commit 4e60785
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/key_value_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::{PgExecutor, PgPool};
use tracing::debug;
use tracing::trace;

pub async fn get_value(executor: impl PgExecutor<'_>, key: &str) -> Option<Value> {
debug!(key = key, "getting key value pair");
trace!(key = key, "getting key value pair");

sqlx::query!(
"
Expand All @@ -21,7 +21,7 @@ pub async fn get_value(executor: impl PgExecutor<'_>, key: &str) -> Option<Value
}

pub async fn set_value(executor: impl PgExecutor<'_>, key: &str, value: &Value) {
debug!("storing key: {}", &key,);
trace!("storing key: {}", &key,);

sqlx::query!(
"
Expand Down
30 changes: 23 additions & 7 deletions src/serve/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
sync::{Arc, RwLock},
};
use tokio::task::JoinHandle;
use tracing::{debug, trace, warn};
use tracing::{debug, info, trace, warn};

use crate::{
caching::{self, CacheKey, ParseCacheKeyError},
Expand All @@ -29,18 +29,34 @@ use super::{State, StateExtension};
pub struct Cache(RwLock<HashMap<CacheKey, Value>>);

impl Cache {
pub async fn new(key_value_store: &impl KeyValueStore) -> Self {
let map = RwLock::new(HashMap::new());
pub fn new() -> Self {
Self(RwLock::new(HashMap::new()))
}

async fn load_from_db(&self, key_value_store: &impl KeyValueStore) {
info!("loading cache from DB");

// Tries to fetch a value from the key value store for every cached analysis value.
for key in all::<CacheKey>().collect::<Vec<_>>() {
let value = caching::get_serialized_caching_value(key_value_store, &key).await;
if let Some(value) = value {
map.write().unwrap().insert(key, value);
match value {
Some(value) => {
let length = serde_json::to_string(&value)
.expect("expect db cache value to be convertable to string")
.len();
debug!(%key, %length, "loaded from DB");
self.0.write().unwrap().insert(key, value);
}
None => {
warn!(%key, "no value found in DB");
}
}
}
}

Self(map)
pub async fn new_with_data(key_value_store: &impl KeyValueStore) -> Self {
let cache = Self::new();
cache.load_from_db(key_value_store).await;
cache
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub async fn start_server() {

debug!("warming cache");

let cache = Cache::new(&key_value_store).await;
let cache = Cache::new_with_data(&key_value_store).await;

info!("cache ready");

Expand Down

0 comments on commit 4e60785

Please sign in to comment.