From e7b29509e56b1adadbe847e20870cbe406a81b08 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 20 Jun 2020 22:40:08 -0700 Subject: [PATCH] Use cache for thumbnail too. --- src/library.rs | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/library.rs b/src/library.rs index 2a44d98..d257373 100644 --- a/src/library.rs +++ b/src/library.rs @@ -137,7 +137,10 @@ impl Library { let mut c = c.lock().unwrap(); match c.get(media_items_id) { Some(bytes) => { - info!("saving local copy from cache {}", media_items_id); + info!( + "saving local copy of original from cache {}", + media_items_id + ); fs::write(&download_path, bytes)?; } None => { @@ -220,17 +223,36 @@ impl Library { Ok(Some(bytes)) => Some(bytes), // Cache miss, fill cache and return. Ok(None) => { - info!("cache MISS {}", key); - let bytes = match self.generate_thumbnail( - media_items_id, - dimensions, - FilterType::Builtin(imageops::FilterType::Lanczos3), - fill, - ) { - Ok(bytes) => bytes, - Err(e) => { - error!("Failed to generate thumbnail for {}: {}", media_items_id, e); - return None; + // TODO(wathiede): use cache for thumbnail like download_image does. + let c = Arc::clone(&self.image_cache); + let mut c = c.lock().unwrap(); + let bytes = match c.get(&key) { + Some(bytes) => { + info!( + "saving local copy of thumbnail from cache {}", + media_items_id + ); + bytes + } + None => { + info!("cache MISS {}", key); + let bytes = match self.generate_thumbnail( + media_items_id, + dimensions, + FilterType::Builtin(imageops::FilterType::Lanczos3), + fill, + ) { + Ok(bytes) => bytes, + Err(e) => { + error!( + "Failed to generate thumbnail for {}: {}", + media_items_id, e + ); + return None; + } + }; + c.set(&key, &bytes); + bytes } }; match db.put(key.as_bytes(), &bytes) {