-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comments because i'm definitely going to forget how this works fix formatting
- Loading branch information
1 parent
ed67a48
commit db5dc31
Showing
7 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::db::{list_origins, Origin}; | ||
use parking_lot::RwLock; | ||
use sqlx::SqlitePool; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use crate::error::AppError; | ||
|
||
#[derive(Debug)] | ||
pub struct OriginCache { | ||
origins: Arc<RwLock<HashMap<String, Origin>>>, | ||
pool: Arc<SqlitePool>, | ||
} | ||
|
||
impl OriginCache { | ||
pub fn new(pool: Arc<SqlitePool>) -> Self { | ||
OriginCache { | ||
origins: Arc::new(RwLock::new(HashMap::new())), | ||
pool, | ||
} | ||
} | ||
|
||
pub async fn refresh(&self) -> Result<(), AppError> { | ||
// Fetch the latest origin data from the database using the provided SqlitePool | ||
let new_origins = list_origins(&self.pool).await?; | ||
|
||
// Create a new HashMap to store the updated origin data | ||
let mut map = HashMap::new(); | ||
|
||
// Iterate over the fetched origins and insert them into the map | ||
for origin in new_origins { | ||
map.insert(origin.domain.clone(), origin); | ||
} | ||
|
||
// Update the cache by acquiring a write lock and replacing the HashMap | ||
*self.origins.write() = map; | ||
Ok(()) | ||
} | ||
|
||
pub async fn get(&self, domain: &str) -> Option<Origin> { | ||
tracing::info!("Get called on cache for domain: {}", domain); | ||
let origins = self.origins.read(); | ||
|
||
// Look up domain in the cache and clone if found | ||
let result = origins.get(domain).cloned(); | ||
|
||
// Mostly for development, but also useful if you want to see how often the cache is hit | ||
if result.is_some() { | ||
tracing::info!("Found origin in cache"); | ||
} else { | ||
tracing::info!("Origin not found in cache"); | ||
} | ||
|
||
// Return the result if found, otherwise None | ||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod cache; | ||
pub mod db; | ||
pub mod error; | ||
pub mod ingest; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters