diff options
| author | Fuwn <[email protected]> | 2022-04-18 04:17:35 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-04-18 04:17:35 -0700 |
| commit | 78f3a233ca24d4df123f2da35897f3e5eec50fc8 (patch) | |
| tree | 28a5002625a1da57c86bdb6f6980419f0469166e | |
| parent | feat: cache more dynamic routes (diff) | |
| download | locus-78f3a233ca24d4df123f2da35897f3e5eec50fc8.tar.xz locus-78f3a233ca24d4df123f2da35897f3e5eec50fc8.zip | |
refactor: constify many constants
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/route.rs | 4 | ||||
| -rw-r--r-- | src/search.rs | 8 |
3 files changed, 11 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index d529f5c..044002e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,7 @@ use tokio::time::Instant; use windmark::{Response, Router}; use yarte::Template; -const CACHE_RATE: u64 = 60 * 5; +const SEARCH_SIZE: usize = 10; static DATABASE: SyncLazy<Mutex<PickleDb>> = SyncLazy::new(|| { Mutex::new({ @@ -209,7 +209,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { ) .parse_query(&query.0.to_string()) .unwrap(), - &tantivy::collector::TopDocs::with_limit(10), + &tantivy::collector::TopDocs::with_limit(SEARCH_SIZE), ) .unwrap(); diff --git a/src/route.rs b/src/route.rs index 7dfa5e7..7140e52 100644 --- a/src/route.rs +++ b/src/route.rs @@ -22,6 +22,8 @@ use tokio::time::Instant; use crate::ROUTES; +pub const CACHE_RATE: u64 = 60 * 5; + #[derive(Debug)] pub struct Route { pub description: String, @@ -62,7 +64,7 @@ pub fn cache(context: &windmark::returnable::RouteContext<'_>, response: &str) { SyncLazy::new(|| Mutex::new(Instant::now())); if (*LAST_CACHED.lock().unwrap()).elapsed() - >= std::time::Duration::from_secs(crate::CACHE_RATE) + >= std::time::Duration::from_secs(CACHE_RATE) || (*ROUTES.lock().unwrap()) .get(context.url.path()) .is_some_and(|&r| r.text_cache.is_empty()) diff --git a/src/search.rs b/src/search.rs index 5e5bc53..9631885 100644 --- a/src/search.rs +++ b/src/search.rs @@ -21,6 +21,8 @@ use std::{lazy::SyncLazy, sync::Mutex}; use tantivy::schema; use tempfile::TempDir; +const SEARCH_INDEX_SIZE: usize = 10_000_000; + pub static INDEX_PATH: SyncLazy<Mutex<TempDir>> = SyncLazy::new(|| Mutex::new(TempDir::new().unwrap())); pub static SCHEMA: SyncLazy<Mutex<tantivy::schema::Schema>> = @@ -47,7 +49,7 @@ pub static INDEX: SyncLazy<Mutex<tantivy::Index>> = SyncLazy::new(|| { }); pub static INDEX_WRITER: SyncLazy<Mutex<tantivy::IndexWriter>> = SyncLazy::new(|| { - Mutex::new((*INDEX.lock().unwrap()).writer(10_000_000).unwrap()) + Mutex::new((*INDEX.lock().unwrap()).writer(SEARCH_INDEX_SIZE).unwrap()) }); pub fn index() { @@ -84,6 +86,8 @@ pub fn index() { time.elapsed().as_nanos() as f64 / 1_000_000.0 ); - std::thread::sleep(std::time::Duration::from_secs(1)); + std::thread::sleep(std::time::Duration::from_secs( + crate::route::CACHE_RATE, + )); } } |