rename lowmem cache to disk cache

This commit is contained in:
Edward Shen 2021-04-23 17:22:29 -04:00
parent 0918b210ea
commit 141b5b257c
Signed by: edward
GPG key ID: 19182661E818369F
4 changed files with 9 additions and 8 deletions

View file

@ -17,7 +17,7 @@ use tokio_stream::wrappers::ReceiverStream;
use super::{BoxedImageStream, Cache, CacheError, CacheKey, CacheStream, ImageMetadata}; use super::{BoxedImageStream, Cache, CacheError, CacheKey, CacheStream, ImageMetadata};
pub struct LowMemCache { pub struct DiskCache {
disk_path: PathBuf, disk_path: PathBuf,
disk_cur_size: AtomicU64, disk_cur_size: AtomicU64,
db_update_channel_sender: Sender<DbMessage>, db_update_channel_sender: Sender<DbMessage>,
@ -28,7 +28,7 @@ enum DbMessage {
Put(Arc<PathBuf>, u32), Put(Arc<PathBuf>, u32),
} }
impl LowMemCache { impl DiskCache {
/// Constructs a new low memory cache at the provided path and capaci ty. /// Constructs a new low memory cache at the provided path and capaci ty.
/// This internally spawns a task that will wait for filesystem /// This internally spawns a task that will wait for filesystem
/// notifications when a file has been written. /// notifications when a file has been written.
@ -137,7 +137,7 @@ async fn db_listener(
} }
#[async_trait] #[async_trait]
impl Cache for LowMemCache { impl Cache for DiskCache {
async fn get( async fn get(
&self, &self,
key: &CacheKey, key: &CacheKey,

View file

@ -2,7 +2,7 @@ use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc; use std::sync::Arc;
use crate::cache::LowMemCache; use crate::cache::DiskCache;
use super::{BoxedImageStream, Cache, CacheKey, CacheStream, ImageMetadata, MemStream}; use super::{BoxedImageStream, Cache, CacheKey, CacheStream, ImageMetadata, MemStream};
use async_trait::async_trait; use async_trait::async_trait;
@ -13,6 +13,7 @@ use tokio::sync::mpsc::{channel, Sender};
use tokio::sync::Mutex; use tokio::sync::Mutex;
/// Memory accelerated disk cache. Uses an LRU in memory to speed up reads. /// Memory accelerated disk cache. Uses an LRU in memory to speed up reads.
///
pub struct MemoryLruCache { pub struct MemoryLruCache {
inner: Arc<Box<dyn Cache>>, inner: Arc<Box<dyn Cache>>,
cur_mem_size: AtomicU64, cur_mem_size: AtomicU64,
@ -29,7 +30,7 @@ impl MemoryLruCache {
) -> Arc<Box<dyn Cache>> { ) -> Arc<Box<dyn Cache>> {
let (tx, mut rx) = channel(100); let (tx, mut rx) = channel(100);
let new_self = Arc::new(Box::new(Self { let new_self = Arc::new(Box::new(Self {
inner: LowMemCache::new(disk_max_size, disk_path).await, inner: DiskCache::new(disk_max_size, disk_path).await,
cur_mem_size: AtomicU64::new(0), cur_mem_size: AtomicU64::new(0),
mem_cache: Mutex::new(LruCache::unbounded()), mem_cache: Mutex::new(LruCache::unbounded()),
master_sender: tx, master_sender: tx,

2
src/cache/mod.rs vendored
View file

@ -17,7 +17,7 @@ use tokio::io::BufReader;
use tokio::sync::mpsc::Sender; use tokio::sync::mpsc::Sender;
use tokio_util::codec::{BytesCodec, FramedRead}; use tokio_util::codec::{BytesCodec, FramedRead};
pub use disk_cache::LowMemCache; pub use disk_cache::DiskCache;
pub use fs::UpstreamError; pub use fs::UpstreamError;
pub use mem_cache::MemoryLruCache; pub use mem_cache::MemoryLruCache;

View file

@ -12,7 +12,7 @@ use std::{num::ParseIntError, sync::atomic::Ordering};
use actix_web::rt::{spawn, time, System}; use actix_web::rt::{spawn, time, System};
use actix_web::web::{self, Data}; use actix_web::web::{self, Data};
use actix_web::{App, HttpServer}; use actix_web::{App, HttpServer};
use cache::{Cache, LowMemCache}; use cache::{Cache, DiskCache};
use clap::Clap; use clap::Clap;
use config::CliArgs; use config::CliArgs;
use log::{debug, error, warn, LevelFilter}; use log::{debug, error, warn, LevelFilter};
@ -126,7 +126,7 @@ async fn main() -> Result<(), std::io::Error> {
}); });
let cache: Arc<Box<dyn Cache>> = if low_mem_mode { let cache: Arc<Box<dyn Cache>> = if low_mem_mode {
LowMemCache::new(disk_quota, cache_path.clone()).await DiskCache::new(disk_quota, cache_path.clone()).await
} else { } else {
MemoryLruCache::new(disk_quota, cache_path.clone(), memory_max_size).await MemoryLruCache::new(disk_quota, cache_path.clone(), memory_max_size).await
}; };