Xuanwo commented on code in PR #5906: URL: https://github.com/apache/opendal/pull/5906#discussion_r2020225405
########## core/src/layers/cache.rs: ########## @@ -0,0 +1,206 @@ +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, + args: OpRead, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +struct CacheValue { + rp: RpRead, + // TODO: store Buffer or Bytes? + bytes: Bytes, +} + +#[derive(Debug)] +pub struct CacheAccessor<A: Access> { + inner: A, + // TODO: if cache should be used for other operations (such as stat or list) + // maybe we should create different caches for each operation? + // So the keys and values does not mix + // Although, we could use an enum as the cachekey and as the cache value, but + // this way we wouldn't be making invalid states unrepresentable, as given + // a Read Cache key, there might be a List cached value associated. + cache: Arc<HybridCache<CacheKey, CacheValue>>, Review Comment: Agreed. Let's focus on reading first. -- 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