Fix updating db timestamp

This commit is contained in:
Edward Shen 2021-04-22 21:29:26 -04:00
parent 288872e84b
commit 758b0ec78d
Signed by: edward
GPG key ID: 19182661E818369F

26
src/cache/low_mem.rs vendored
View file

@ -25,8 +25,8 @@ pub struct LowMemCache {
} }
enum DbMessage { enum DbMessage {
Get(Arc<CacheKey>), Get(Arc<PathBuf>),
Put(PathBuf, u32), Put(Arc<PathBuf>, u32),
} }
impl LowMemCache { impl LowMemCache {
@ -102,7 +102,7 @@ impl LowMemCache {
for message in messages { for message in messages {
match message { match message {
DbMessage::Get(entry) => { DbMessage::Get(entry) => {
let key = entry.to_string(); let key = entry.as_os_str().to_str();
let query = sqlx::query!( let query = sqlx::query!(
"update Images set accessed = ? where id = ?", "update Images set accessed = ? where id = ?",
now, now,
@ -111,7 +111,7 @@ impl LowMemCache {
.execute(&mut transaction) .execute(&mut transaction)
.await; .await;
if let Err(e) = query { if let Err(e) = query {
warn!("Failed to update timestamp in db for {}: {}", key, e); warn!("Failed to update timestamp in db for {:?}: {}", key, e);
} }
} }
DbMessage::Put(entry, size) => { DbMessage::Put(entry, size) => {
@ -145,14 +145,16 @@ impl Cache for LowMemCache {
key: Arc<CacheKey>, key: Arc<CacheKey>,
) -> Option<Result<(CacheStream, ImageMetadata), CacheError>> { ) -> Option<Result<(CacheStream, ImageMetadata), CacheError>> {
let channel = self.db_update_channel_sender.clone(); let channel = self.db_update_channel_sender.clone();
let key_0 = Arc::clone(&key);
tokio::spawn(async move { channel.send(DbMessage::Get(key_0)).await }); let path = Arc::new(
self.disk_path
let path = self
.disk_path
.clone() .clone()
.join(PathBuf::from(Arc::clone(&key).as_ref())); .join(PathBuf::from(Arc::clone(&key).as_ref())),
);
let path_0 = Arc::clone(&path);
tokio::spawn(async move { channel.send(DbMessage::Get(path_0)).await });
super::fs::read_file(&path) super::fs::read_file(&path)
.await .await
.map(|res| res.map_err(Into::into)) .map(|res| res.map_err(Into::into))
@ -166,8 +168,8 @@ impl Cache for LowMemCache {
) -> Result<CacheStream, CacheError> { ) -> Result<CacheStream, CacheError> {
let channel = self.db_update_channel_sender.clone(); let channel = self.db_update_channel_sender.clone();
let path = self.disk_path.clone().join(PathBuf::from(key.as_ref())); let path = Arc::new(self.disk_path.clone().join(PathBuf::from(key.as_ref())));
let path_0 = path.clone(); let path_0 = Arc::clone(&path);
let db_callback = |size: u32| async move { let db_callback = |size: u32| async move {
let _ = channel.send(DbMessage::Put(path_0, size)).await; let _ = channel.send(DbMessage::Put(path_0, size)).await;