Is there a crate for a concurrent cache like this?

For a cache I'd like to have a data structure similar to a hashmap that works like this:

I can request an entry by key. If the key exists in the cache, a copy/reference of the value is returned. If the key does not exist, the entry is locked and I get some kind of guard representing the lock. I can then work on calculating the cache entry and insert it into the cache. If any other thread tries to request that key in the meantime, that call will block until the first thread inserts the cache entry.

I'm pretty sure this is a common problem, but I don't know the name of this pattern, so it's difficult to check whether a crate already exists for this problem.

I think WaitCache — Rust caching library // Lib.rs is what you want.

5 Likes

Yesss, exactly.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.