| 1 | use chrono::{DateTime, Utc}; |
| 2 | use rusqlite::{OptionalExtension, params}; |
| 3 | |
| 4 | use crate::error::Result; |
| 5 | use crate::models::ImageRecord; |
| 6 | use crate::store::Store; |
| 7 | use crate::time::{from_millis, to_millis}; |
| 8 | |
| 9 | impl Store { |
| 10 | pub fn image(&self, key: &str) -> Result<Option<ImageRecord>> { |
| 11 | let connection = self.lock()?; |
| 12 | Ok(connection |
| 13 | .query_row( |
| 14 | "SELECT key, bytes, content_type, fetched_at FROM images WHERE key = ?1", |
| 15 | [key], |
| 16 | |row| { |
| 17 | Ok(ImageRecord { |
| 18 | key: row.get(0)?, |
| 19 | bytes: row.get(1)?, |
| 20 | content_type: row.get(2)?, |
| 21 | fetched_at: from_millis(row.get(3)?), |
| 22 | }) |
| 23 | }, |
| 24 | ) |
| 25 | .optional()?) |
| 26 | } |
| 27 | |
| 28 | pub fn has_image(&self, key: &str) -> Result<bool> { |
| 29 | let connection = self.lock()?; |
| 30 | Ok(connection |
| 31 | .query_row("SELECT 1 FROM images WHERE key = ?1", [key], |_| Ok(())) |
| 32 | .optional()? |
| 33 | .is_some()) |
| 34 | } |
| 35 | |
| 36 | /// Stores the image, then evicts the oldest others until the total is within `max_total_bytes`. |
| 37 | pub fn put_image(&self, image: &ImageRecord, max_total_bytes: u64) -> Result<()> { |
| 38 | let mut connection = self.lock()?; |
| 39 | let transaction = connection.transaction()?; |
| 40 | transaction.execute( |
| 41 | "INSERT INTO images (key, bytes, content_type, fetched_at, size) VALUES (?1, ?2, ?3, ?4, ?5) |
| 42 | ON CONFLICT (key) DO UPDATE SET |
| 43 | bytes = excluded.bytes, content_type = excluded.content_type, |
| 44 | fetched_at = excluded.fetched_at, size = excluded.size", |
| 45 | params![ |
| 46 | image.key, |
| 47 | image.bytes, |
| 48 | image.content_type, |
| 49 | to_millis(image.fetched_at), |
| 50 | image.bytes.len() as i64 |
| 51 | ], |
| 52 | )?; |
| 53 | let mut total: i64 = |
| 54 | transaction.query_row("SELECT COALESCE(SUM(size), 0) FROM images", [], |row| { |
| 55 | row.get(0) |
| 56 | })?; |
| 57 | if total > max_total_bytes as i64 { |
| 58 | let oldest = transaction |
| 59 | .prepare( |
| 60 | "SELECT key, size FROM images WHERE key != ?1 ORDER BY fetched_at ASC, key ASC", |
| 61 | )? |
| 62 | .query_map([&image.key], |row| { |
| 63 | Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?)) |
| 64 | })? |
| 65 | .collect::<rusqlite::Result<Vec<_>>>()?; |
| 66 | for (key, size) in oldest { |
| 67 | if total <= max_total_bytes as i64 { |
| 68 | break; |
| 69 | } |
| 70 | transaction.execute("DELETE FROM images WHERE key = ?1", [key])?; |
| 71 | total -= size; |
| 72 | } |
| 73 | } |
| 74 | Ok(transaction.commit()?) |
| 75 | } |
| 76 | |
| 77 | pub fn image_cache_bytes(&self) -> Result<u64> { |
| 78 | let connection = self.lock()?; |
| 79 | let total: i64 = |
| 80 | connection.query_row("SELECT COALESCE(SUM(size), 0) FROM images", [], |row| { |
| 81 | row.get(0) |
| 82 | })?; |
| 83 | Ok(total as u64) |
| 84 | } |
| 85 | |
| 86 | pub fn delete_images_before(&self, cutoff: DateTime<Utc>) -> Result<usize> { |
| 87 | let connection = self.lock()?; |
| 88 | Ok(connection.execute( |
| 89 | "DELETE FROM images WHERE fetched_at < ?1", |
| 90 | [to_millis(cutoff)], |
| 91 | )?) |
| 92 | } |
| 93 | } |