opendal icon indicating copy to clipboard operation
opendal copied to clipboard

new feature: Add cache layer for opendal

Open Xuanwo opened this issue 9 months ago • 14 comments

Provide high-quality built-in cache support in OpenDAL while ensuring users have the flexibility to implement their own caching logic.

Xuanwo avatar Mar 01 '25 10:03 Xuanwo

Hi, I would like to work on this, can I? Or is anybody willing to work on this already?

I thought about using https://github.com/foyer-rs/foyer. I see It has been already discussed here.

jorgehermo9 avatar Mar 05 '25 23:03 jorgehermo9

Hi, @jorgehermo9, that will be greatly appreciated. No one is currently working on this.


Ideally, our cache layer should enable users to design and implement their own cache algorithms while seamlessly switching between different cache crates like Foyer or Moka. However, I believe we should integrate with Foyer first to clarify our direction moving forward.

Let's first build a FoyerLayer, and later, we can extract a CacheLayer from it.

Xuanwo avatar Mar 06 '25 04:03 Xuanwo

I agree a lot with the plan!! Thanks @xuanwo 😊

jorgehermo9 avatar Mar 06 '25 07:03 jorgehermo9

Just as an update about this, I have the initial implementation almost finished (for read and blocking_read operations for the moment) and will submit a PR in a few days/a week, as soon as I can šŸ˜„

jorgehermo9 avatar Mar 23 '25 22:03 jorgehermo9

I have an idea that our future CacheLayer can accept an Operator instead. Take foyer as an example, we can implement a foyer service. And use it like:

let cache_op = Operator::from_config(FoyerConfig::default).finish()?;
let op = op.layer(CacheLayer::new(cache_op));

We will need a benchmark to evaluate their performance.

Xuanwo avatar Apr 17 '25 05:04 Xuanwo

I am currently trying to write this CacheLayer (to be used in some multi-layer caching scenarios, such as memory + redis). I might consider submitting a draft PR later this week to discuss some implementation details.

In fact, I have implemented a simple cache layer, customized the cache logic through a CacheStorage trait, and implemented this trait for Operator, now we can build an operator with a cache layer in the following way:

let moka_builder = <MokaBuilder>::default().name("demo1").time_to_live(Duration::from_secs(1));
let moka1 = Operator::new(moka_builder)?.layer(LoggingLayer::default()).finish();

let moka_builder = <MokaBuilder>::default().name("demo2").time_to_live(Duration::from_secs(5));
let moka2 = Operator::new(moka_builder)?.layer(LoggingLayer::default()).finish();
let op = Operator::new(NoopBuilder)?
    .layer(CacheLayer::new(moka2))
    .layer(CacheLayer::new(moka1))
    .finish();

koushiro avatar Jun 10 '25 06:06 koushiro

I am currently trying to write this CacheLayer (to be used in some multi-layer caching scenarios, such as memory + redis). I might consider submitting a draft PR later this week to discuss some implementation details.

Hi, I'd prefer if we could have an RFC first before getting into implementation details. I'm not sure we're on the same page yet.

By the way, @MrCroxx is also working this. Maybe we can have a small talk around it.

Xuanwo avatar Jun 10 '25 06:06 Xuanwo

Hi @xuanwo, do you expect me to continue in my foyer implementation PR or should I it hand off to @MrCroxx's work?

I mean, if my PR was not what you expected, I could stop working on it

jorgehermo9 avatar Jun 10 '25 06:06 jorgehermo9

Hi, I'd prefer if we could have an RFC first before getting into implementation details. I'm not sure we're on the same page yet.

By the way, @MrCroxx is also working this. Maybe we can have a small talk around it.

This PR is just a prototype (mainly used to discuss some designs, not the final implementation), I will also consider writing a related RFC (maybe after some discussion?)

I’d be happy to have a small talk about it.

koushiro avatar Jun 10 '25 06:06 koushiro

Hi @Xuanwo, do you expect me to continue in my foyer implementation PR or should I it hand off to @MrCroxx's work?

I mean, if my PR was not what you expected, I could stop working on it

Thank you for your experiment! I think we shouldn't have a foyer layer. Instead, we should have a cache layer along with a foyer service. Are you interested in updating your PR to implement a foyer service instead?

Xuanwo avatar Jun 10 '25 06:06 Xuanwo

No problem @Xuanwo, I think you are right and I have the same opinion about this.

Are you interested in updating your PR to implement a foyer service instead?

I don't think there is anything on that PR that we can reuse for the service... I think it would be better to start from scratch though, I can't confirm if I have enough time to work on that also! Feel free to close the PR if you want

jorgehermo9 avatar Jun 10 '25 07:06 jorgehermo9

I don't think there is anything on that PR that we can reuse for the service... I think it would be better to start from scratch though, I can't confirm if I have enough time to work on that also! Feel free to close the PR if you want

Sorry about this. I have learned, experimented, and thought a lot based on your PR. If there is anything that makes the opendal cache layer looks great, your contribution is definitely the most important.

I will find other ways to recognize your contribution, even if it can't be merged as a dedicated PR.

Xuanwo avatar Jun 10 '25 07:06 Xuanwo

dont apoligize @xuanwo! I also learned a lot while working on it. Thank you very much for your kind words :)

jorgehermo9 avatar Jun 11 '25 06:06 jorgehermo9

I've proposed a new RFC for the cache layer, and I'd appreciate if you could give me some feedback, this is my first time proposing an RFC so I don't know if there's anything I did wrong :P

koushiro avatar Jun 16 '25 13:06 koushiro