Skip to content

Commit 669ca04

Browse files
xis19sylvestre
andauthored
Extract LazyDiskCache to a separated file (#2573)
--------- Co-authored-by: Sylvestre Ledru <sledru@mozilla.com> Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 316b807 commit 669ca04

File tree

3 files changed

+59
-40
lines changed

3 files changed

+59
-40
lines changed

src/cache/disk.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,19 @@
1414

1515
use crate::cache::{Cache, CacheMode, CacheRead, CacheWrite, Storage};
1616
use crate::compiler::PreprocessorCacheEntry;
17-
use crate::lru_disk_cache::LruDiskCache;
1817
use crate::lru_disk_cache::{Error as LruError, ReadSeek};
1918
use async_trait::async_trait;
20-
use std::ffi::{OsStr, OsString};
19+
use std::ffi::OsStr;
2120
use std::io::{BufWriter, Write};
2221
use std::path::{Path, PathBuf};
2322
use std::sync::{Arc, Mutex};
2423
use std::time::{Duration, Instant};
2524

2625
use crate::errors::*;
2726

27+
use super::lazy_disk_cache::LazyDiskCache;
2828
use super::{PreprocessorCacheModeConfig, normalize_key};
2929

30-
enum LazyDiskCache {
31-
Uninit { root: OsString, max_size: u64 },
32-
Init(LruDiskCache),
33-
}
34-
35-
impl LazyDiskCache {
36-
fn get_or_init(&mut self) -> Result<&mut LruDiskCache> {
37-
match self {
38-
LazyDiskCache::Uninit { root, max_size } => {
39-
*self = LazyDiskCache::Init(LruDiskCache::new(&root, *max_size)?);
40-
self.get_or_init()
41-
}
42-
LazyDiskCache::Init(d) => Ok(d),
43-
}
44-
}
45-
46-
fn get(&mut self) -> Option<&mut LruDiskCache> {
47-
match self {
48-
LazyDiskCache::Uninit { .. } => None,
49-
LazyDiskCache::Init(d) => Some(d),
50-
}
51-
}
52-
53-
fn capacity(&self) -> u64 {
54-
match self {
55-
LazyDiskCache::Uninit { max_size, .. } => *max_size,
56-
LazyDiskCache::Init(d) => d.capacity(),
57-
}
58-
}
59-
60-
fn path(&self) -> &Path {
61-
match self {
62-
LazyDiskCache::Uninit { root, .. } => root.as_ref(),
63-
LazyDiskCache::Init(d) => d.path(),
64-
}
65-
}
66-
}
67-
6830
/// A cache that stores entries at local disk paths.
6931
pub struct DiskCache {
7032
/// `LruDiskCache` does all the real work here.

src/cache/lazy_disk_cache.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
use crate::errors::*;
14+
use crate::lru_disk_cache::LruDiskCache;
15+
use std::ffi::OsString;
16+
use std::path::Path;
17+
18+
pub enum LazyDiskCache {
19+
Uninit { root: OsString, max_size: u64 },
20+
Init(LruDiskCache),
21+
}
22+
23+
impl LazyDiskCache {
24+
pub fn get_or_init(&mut self) -> Result<&mut LruDiskCache> {
25+
match self {
26+
LazyDiskCache::Uninit { root, max_size } => {
27+
*self = LazyDiskCache::Init(LruDiskCache::new(&root, *max_size)?);
28+
self.get_or_init()
29+
}
30+
LazyDiskCache::Init(d) => Ok(d),
31+
}
32+
}
33+
34+
pub fn get(&mut self) -> Option<&mut LruDiskCache> {
35+
match self {
36+
LazyDiskCache::Uninit { .. } => None,
37+
LazyDiskCache::Init(d) => Some(d),
38+
}
39+
}
40+
41+
pub fn capacity(&self) -> u64 {
42+
match self {
43+
LazyDiskCache::Uninit { max_size, .. } => *max_size,
44+
LazyDiskCache::Init(d) => d.capacity(),
45+
}
46+
}
47+
48+
pub fn path(&self) -> &Path {
49+
match self {
50+
LazyDiskCache::Uninit { root, .. } => root.as_ref(),
51+
LazyDiskCache::Init(d) => d.path(),
52+
}
53+
}
54+
}

src/cache/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub mod disk;
2323
pub mod gcs;
2424
#[cfg(feature = "gha")]
2525
pub mod gha;
26+
#[allow(clippy::module_inception)]
27+
pub mod lazy_disk_cache;
2628
#[cfg(feature = "memcached")]
2729
pub mod memcached;
2830
#[cfg(feature = "oss")]
@@ -47,3 +49,4 @@ pub mod webdav;
4749
pub(crate) mod http_client;
4850

4951
pub use crate::cache::cache::*;
52+
pub use crate::cache::lazy_disk_cache::*;

0 commit comments

Comments
 (0)