Skip to content

Commit

Permalink
Reduce the number of lock and release on origins
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinMoss committed Jul 2, 2023
1 parent e580a17 commit 43e5c5d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ impl OriginCacheInner {
}

pub fn refresh(&self, new_origins: Vec<Origin>) -> Result<(), AppError> {
// 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);
}

//*self.origins.write() = map;
let map = new_origins.into_iter()
.map(|origin| (origin.domain.clone(), origin))
.collect();

// Update the cache by acquiring a write lock and replacing the HashMap
*self.origins.write() = map;
Expand All @@ -60,7 +59,11 @@ impl OriginCacheInner {
let origins = self.origins.read();

// Look up domain in the cache and clone if found
let result = origins.get(domain).cloned();
let result = {
let origins = self.origins.read();

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() {
Expand Down

0 comments on commit 43e5c5d

Please sign in to comment.