v0.2.3 - cache micro-optimization
This commit is contained in:
		
							parent
							
								
									51c4b72626
								
							
						
					
					
						commit
						9109f822eb
					
				|  | @ -246,7 +246,7 @@ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" | |||
| 
 | ||||
| [[package]] | ||||
| name = "breeze" | ||||
| version = "0.2.2" | ||||
| version = "0.2.3" | ||||
| dependencies = [ | ||||
|  "anyhow", | ||||
|  "async-recursion", | ||||
|  |  | |||
|  | @ -1,6 +1,6 @@ | |||
| [package] | ||||
| name = "breeze" | ||||
| version = "0.2.2" | ||||
| version = "0.2.3" | ||||
| edition = "2021" | ||||
| 
 | ||||
| [dependencies] | ||||
|  |  | |||
							
								
								
									
										32
									
								
								src/cache.rs
								
								
								
								
							
							
						
						
									
										32
									
								
								src/cache.rs
								
								
								
								
							|  | @ -178,9 +178,10 @@ impl Cache { | |||
|             // drop the reference so we don't deadlock
 | ||||
|             drop(e); | ||||
| 
 | ||||
|             // remove it and say we never had it
 | ||||
|             // remove it
 | ||||
|             self.remove(key); | ||||
| 
 | ||||
|             // and say we never had it
 | ||||
|             return None; | ||||
|         } | ||||
| 
 | ||||
|  | @ -230,14 +231,29 @@ impl Cache { | |||
|             // If we don't do this it'll be a LOT of system api calls
 | ||||
|             let now = SystemTime::now(); | ||||
| 
 | ||||
|             // Drop every expired entry
 | ||||
|             // If we fail to compare the times, we drop the entry
 | ||||
|             self.map.retain(|_, e| { | ||||
|                 let elapsed = now.duration_since(e.last_used()).unwrap_or(Duration::MAX); | ||||
|                 let is_expired = elapsed >= e.lifetime; | ||||
|             // Collect a list of all the expired keys
 | ||||
|             // If we fail to compare the times, it gets added to the list anyways
 | ||||
|             let expired: Vec<_> = self | ||||
|                 .map | ||||
|                 .par_iter() | ||||
|                 .filter_map(|e| { | ||||
|                     let elapsed = now.duration_since(e.last_used()).unwrap_or(Duration::MAX); | ||||
|                     let is_expired = elapsed >= e.lifetime; | ||||
| 
 | ||||
|                 !is_expired | ||||
|             }) | ||||
|                     if is_expired { | ||||
|                         Some(e.key().clone()) | ||||
|                     } else { | ||||
|                         None | ||||
|                     } | ||||
|                 }) | ||||
|                 .collect(); | ||||
| 
 | ||||
|             // If we have any, lock the map and drop all of them
 | ||||
|             if !expired.is_empty() { | ||||
|                 // Use a retain call, should be less locks that way
 | ||||
|                 // (instead of many remove calls)
 | ||||
|                 self.map.retain(|k, _| !expired.contains(k)) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -8,14 +8,17 @@ pub async fn index(State(engine): State<Arc<crate::engine::Engine>>) -> String { | |||
| 
 | ||||
|     let motd = engine.cfg.motd.clone(); | ||||
| 
 | ||||
|     motd | ||||
|         .replace("%version%", env!("CARGO_PKG_VERSION")) | ||||
|     motd.replace("%version%", env!("CARGO_PKG_VERSION")) | ||||
|         .replace("%uplcount%", &count.to_string()) | ||||
| } | ||||
| 
 | ||||
| pub async fn robots_txt() -> &'static str { | ||||
|     /// robots.txt that tells web crawlers not to list uploads
 | ||||
|     const ROBOTS_TXT: &str = concat!("User-Agent: *\n", "Disallow: /p/*\n", "Allow: /\n"); | ||||
|     const ROBOTS_TXT: &str = concat!( | ||||
|         "User-Agent: *\n", | ||||
|         "Disallow: /p/*\n", | ||||
|         "Allow: /\n" | ||||
|     ); | ||||
| 
 | ||||
|     ROBOTS_TXT | ||||
| } | ||||
|  |  | |||
|  | @ -1,6 +1,4 @@ | |||
| use std::{ | ||||
|     ffi::OsStr, path::PathBuf, sync::Arc | ||||
| }; | ||||
| use std::{ffi::OsStr, path::PathBuf, sync::Arc}; | ||||
| 
 | ||||
| use axum::{ | ||||
|     body::Body, | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue