Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions src/cache/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,19 @@

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};
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.
Expand Down
54 changes: 54 additions & 0 deletions src/cache/lazy_disk_cache.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}
}
3 changes: 3 additions & 0 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -47,3 +49,4 @@ pub mod webdav;
pub(crate) mod http_client;

pub use crate::cache::cache::*;
pub use crate::cache::lazy_disk_cache::*;
Loading