diff --git a/src/cache/disk.rs b/src/cache/disk.rs index 7e75100b1..9f5167901 100644 --- a/src/cache/disk.rs +++ b/src/cache/disk.rs @@ -14,10 +14,9 @@ use crate::cache::{Cache, CacheMode, CacheRead, CacheWrite, Storage}; use crate::compiler::PreprocessorCacheEntry; -use crate::lru_disk_cache::LruDiskCache; use crate::lru_disk_cache::{Error as LruError, ReadSeek}; use async_trait::async_trait; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsStr; use std::io::{BufWriter, Write}; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex}; @@ -25,46 +24,9 @@ use std::time::{Duration, Instant}; use crate::errors::*; +use super::lazy_disk_cache::LazyDiskCache; use super::{PreprocessorCacheModeConfig, normalize_key}; -enum LazyDiskCache { - Uninit { root: OsString, max_size: u64 }, - Init(LruDiskCache), -} - -impl LazyDiskCache { - fn get_or_init(&mut self) -> Result<&mut LruDiskCache> { - match self { - LazyDiskCache::Uninit { root, max_size } => { - *self = LazyDiskCache::Init(LruDiskCache::new(&root, *max_size)?); - self.get_or_init() - } - LazyDiskCache::Init(d) => Ok(d), - } - } - - fn get(&mut self) -> Option<&mut LruDiskCache> { - match self { - LazyDiskCache::Uninit { .. } => None, - LazyDiskCache::Init(d) => Some(d), - } - } - - fn capacity(&self) -> u64 { - match self { - LazyDiskCache::Uninit { max_size, .. } => *max_size, - LazyDiskCache::Init(d) => d.capacity(), - } - } - - fn path(&self) -> &Path { - match self { - LazyDiskCache::Uninit { root, .. } => root.as_ref(), - LazyDiskCache::Init(d) => d.path(), - } - } -} - /// A cache that stores entries at local disk paths. pub struct DiskCache { /// `LruDiskCache` does all the real work here. diff --git a/src/cache/lazy_disk_cache.rs b/src/cache/lazy_disk_cache.rs new file mode 100644 index 000000000..0963522f6 --- /dev/null +++ b/src/cache/lazy_disk_cache.rs @@ -0,0 +1,54 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::errors::*; +use crate::lru_disk_cache::LruDiskCache; +use std::ffi::OsString; +use std::path::Path; + +pub enum LazyDiskCache { + Uninit { root: OsString, max_size: u64 }, + Init(LruDiskCache), +} + +impl LazyDiskCache { + pub fn get_or_init(&mut self) -> Result<&mut LruDiskCache> { + match self { + LazyDiskCache::Uninit { root, max_size } => { + *self = LazyDiskCache::Init(LruDiskCache::new(&root, *max_size)?); + self.get_or_init() + } + LazyDiskCache::Init(d) => Ok(d), + } + } + + pub fn get(&mut self) -> Option<&mut LruDiskCache> { + match self { + LazyDiskCache::Uninit { .. } => None, + LazyDiskCache::Init(d) => Some(d), + } + } + + pub fn capacity(&self) -> u64 { + match self { + LazyDiskCache::Uninit { max_size, .. } => *max_size, + LazyDiskCache::Init(d) => d.capacity(), + } + } + + pub fn path(&self) -> &Path { + match self { + LazyDiskCache::Uninit { root, .. } => root.as_ref(), + LazyDiskCache::Init(d) => d.path(), + } + } +} diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 744499414..c23be2304 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -23,6 +23,8 @@ pub mod disk; pub mod gcs; #[cfg(feature = "gha")] pub mod gha; +#[allow(clippy::module_inception)] +pub mod lazy_disk_cache; #[cfg(feature = "memcached")] pub mod memcached; #[cfg(feature = "oss")] @@ -47,3 +49,4 @@ pub mod webdav; pub(crate) mod http_client; pub use crate::cache::cache::*; +pub use crate::cache::lazy_disk_cache::*;