Skip to content

Commit b4c746f

Browse files
Felixoidsylvestre
authored andcommitted
Add a helper method to get a correct backend name
1 parent 4e9f501 commit b4c746f

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/cache/cache.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ pub trait Storage: Send + Sync {
373373
/// Get the storage location.
374374
fn location(&self) -> String;
375375

376+
/// Get the cache backend type name (e.g., "disk", "redis", "s3").
377+
/// Used for statistics and display purposes.
378+
fn cache_type_name(&self) -> &'static str {
379+
"unknown"
380+
}
381+
376382
/// Get the current storage usage, if applicable.
377383
async fn current_size(&self) -> Result<Option<u64>>;
378384

@@ -588,6 +594,12 @@ impl Storage for RemoteStorage {
588594
)
589595
}
590596

597+
fn cache_type_name(&self) -> &'static str {
598+
// Use opendal's scheme as the cache type name
599+
// This returns "s3", "redis", "azure", "gcs", etc.
600+
self.operator.info().scheme()
601+
}
602+
591603
async fn current_size(&self) -> Result<Option<u64>> {
592604
Ok(None)
593605
}

src/cache/disk.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ impl Storage for DiskCache {
137137
format!("Local disk: {:?}", self.lru.lock().unwrap().path())
138138
}
139139

140+
fn cache_type_name(&self) -> &'static str {
141+
"disk"
142+
}
143+
140144
async fn current_size(&self) -> Result<Option<u64>> {
141145
Ok(self.lru.lock().unwrap().get().map(|l| l.size()))
142146
}
@@ -185,3 +189,27 @@ impl Storage for DiskCache {
185189
.commit(f)?)
186190
}
187191
}
192+
193+
#[cfg(test)]
194+
mod tests {
195+
use super::*;
196+
197+
#[test]
198+
fn test_disk_cache_type_name() {
199+
let tempdir = tempfile::tempdir().unwrap();
200+
let runtime = tokio::runtime::Builder::new_current_thread()
201+
.build()
202+
.unwrap();
203+
204+
let disk = DiskCache::new(
205+
tempdir.path(),
206+
1024 * 1024,
207+
runtime.handle(),
208+
PreprocessorCacheModeConfig::default(),
209+
CacheMode::ReadWrite,
210+
vec![],
211+
);
212+
213+
assert_eq!(disk.cache_type_name(), "disk");
214+
}
215+
}

src/cache/readonly.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ impl Storage for ReadOnlyStorage {
4949
self.0.location()
5050
}
5151

52+
/// Get the cache backend type name.
53+
fn cache_type_name(&self) -> &'static str {
54+
self.0.cache_type_name()
55+
}
56+
5257
/// Get the current storage usage, if applicable.
5358
async fn current_size(&self) -> Result<Option<u64>> {
5459
self.0.current_size().await
@@ -188,4 +193,33 @@ mod test {
188193
);
189194
});
190195
}
196+
197+
#[test]
198+
fn readonly_storage_forwards_cache_type_name() {
199+
let runtime = tokio::runtime::Builder::new_current_thread()
200+
.enable_all()
201+
.worker_threads(1)
202+
.build()
203+
.unwrap();
204+
205+
let tempdir = tempfile::Builder::new()
206+
.prefix("readonly_cache_type_name")
207+
.tempdir()
208+
.expect("Failed to create tempdir");
209+
let cache_dir = tempdir.path().join("cache");
210+
std::fs::create_dir(&cache_dir).unwrap();
211+
212+
let disk_cache = crate::cache::disk::DiskCache::new(
213+
&cache_dir,
214+
1024 * 1024,
215+
runtime.handle(),
216+
super::PreprocessorCacheModeConfig::default(),
217+
super::CacheMode::ReadWrite,
218+
vec![],
219+
);
220+
221+
let readonly_storage = ReadOnlyStorage(std::sync::Arc::new(disk_cache));
222+
223+
assert_eq!(readonly_storage.cache_type_name(), "disk");
224+
}
191225
}

0 commit comments

Comments
 (0)