Xuanwo commented on code in PR #5906: URL: https://github.com/apache/opendal/pull/5906#discussion_r2020225604
########## core/src/layers/cache.rs: ########## @@ -0,0 +1,187 @@ +use std::sync::Arc; + +use crate::raw::*; +use crate::*; +use bytes::Bytes; +use foyer::{DirectFsDeviceOptions, Engine, HybridCache, HybridCacheBuilder}; +use serde::{Deserialize, Serialize}; + +pub struct CacheLayer { + cache: Arc<HybridCache<CacheKey, CacheValue>>, +} + +impl CacheLayer { + pub async fn new( + disk_cache_dir: &str, + disk_capacity_mb: usize, + memory_capacity_mb: usize, + ) -> Result<Self> { + const MB: usize = 1 << 20; + + let cache = HybridCacheBuilder::new() + .with_name("opendal") + .memory(memory_capacity_mb * MB) + .storage(Engine::Large) + .with_device_options( + DirectFsDeviceOptions::new(disk_cache_dir).with_capacity(disk_capacity_mb * MB), + ) + .build() + .await + .map_err(|e| Error::new(ErrorKind::Unexpected, e.to_string()))?; + + Ok(Self { + cache: Arc::new(cache), + }) + } +} + +impl<A: Access> Layer<A> for CacheLayer { + type LayeredAccess = CacheAccessor<A>; + + fn layer(&self, inner: A) -> Self::LayeredAccess { + CacheAccessor { + inner, + cache: Arc::clone(&self.cache), + } + } +} + +#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)] +struct CacheKey { + path: String, Review Comment: The cache key could be too large. I suggest we only take required parts as cache key. Most metadata in `OpRead` have not effect on returned data. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@opendal.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org