2021-04-14 19:11:00 -07:00
|
|
|
//! Low memory caching stuff
|
|
|
|
|
2021-04-18 14:06:40 -07:00
|
|
|
use std::path::PathBuf;
|
2021-04-14 19:11:00 -07:00
|
|
|
|
|
|
|
use async_trait::async_trait;
|
2021-04-18 14:38:33 -07:00
|
|
|
use log::warn;
|
2021-04-14 19:11:00 -07:00
|
|
|
use lru::LruCache;
|
|
|
|
|
2021-04-18 14:06:40 -07:00
|
|
|
use super::{BoxedImageStream, Cache, CacheError, CacheKey, CacheStream, ImageMetadata};
|
2021-04-14 19:11:00 -07:00
|
|
|
|
|
|
|
pub struct LowMemCache {
|
2021-04-18 14:06:40 -07:00
|
|
|
on_disk: LruCache<CacheKey, ImageMetadata>,
|
2021-04-14 19:11:00 -07:00
|
|
|
disk_path: PathBuf,
|
2021-04-14 20:44:13 -07:00
|
|
|
disk_max_size: u64,
|
|
|
|
disk_cur_size: u64,
|
2021-04-14 19:11:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LowMemCache {
|
2021-04-14 20:44:13 -07:00
|
|
|
pub fn new(disk_max_size: u64, disk_path: PathBuf) -> Self {
|
2021-04-14 19:11:00 -07:00
|
|
|
Self {
|
|
|
|
on_disk: LruCache::unbounded(),
|
|
|
|
disk_path,
|
|
|
|
disk_max_size,
|
|
|
|
disk_cur_size: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl Cache for LowMemCache {
|
2021-04-18 14:06:40 -07:00
|
|
|
async fn get(
|
|
|
|
&mut self,
|
|
|
|
key: &CacheKey,
|
|
|
|
) -> Option<Result<(CacheStream, &ImageMetadata), CacheError>> {
|
2021-04-18 14:25:28 -07:00
|
|
|
let metadata = self.on_disk.get(key)?;
|
|
|
|
let path = self.disk_path.clone().join(PathBuf::from(key.clone()));
|
2021-04-18 20:06:18 -07:00
|
|
|
super::fs::read_file(&path)
|
|
|
|
.await
|
|
|
|
.map(|res| res.map(|stream| (stream, metadata)).map_err(Into::into))
|
2021-04-14 19:11:00 -07:00
|
|
|
}
|
|
|
|
|
2021-04-18 14:06:40 -07:00
|
|
|
async fn put(
|
2021-04-17 20:19:27 -07:00
|
|
|
&mut self,
|
|
|
|
key: CacheKey,
|
2021-04-18 14:06:40 -07:00
|
|
|
image: BoxedImageStream,
|
|
|
|
metadata: ImageMetadata,
|
|
|
|
) -> Result<(CacheStream, &ImageMetadata), CacheError> {
|
|
|
|
let path = self.disk_path.clone().join(PathBuf::from(key.clone()));
|
|
|
|
self.on_disk.put(key.clone(), metadata);
|
|
|
|
super::fs::write_file(&path, image)
|
|
|
|
.await
|
|
|
|
.map(move |stream| (stream, self.on_disk.get(&key).unwrap()))
|
|
|
|
.map_err(Into::into)
|
2021-04-14 19:11:00 -07:00
|
|
|
}
|
2021-04-18 14:38:33 -07:00
|
|
|
|
|
|
|
async fn prune(&mut self) {
|
|
|
|
warn!("Trimming has not been implemented yet. Cache is unbounded!");
|
|
|
|
}
|
2021-04-14 19:11:00 -07:00
|
|
|
}
|