CrudeCache is a simple utility for good enough concurrent caching needs. Inspired by Play Framework's cache.getOrElseUpdate.
rust
//Returns non-expired cached item. If one does not exist, new item will be cached & returned
cache.get_or_else_update("cache_key", Duration::from_secs(60), async_function).await
Disclaimer: Please note that CrudeCache was developed for personal projects, it is not battle-tested in production and might contain bugs. Feedback, suggestions, and contributions are more than welcome.
ShardedMap, which splits the data across multiple shards (HashMaps). No bells and whistles, and no resharding.tokio::sync::RwLockAdd this to your Cargo.toml:
toml
[dependencies]
crude_cache = "0.1.1"
```rust use crude_cache::CrudeCache;
pub struct DataService {
cache: Arc
impl DataService { pub async fn getbigdata(&self) -> SomeBigType { self.cache.getorelseupdate("bigdata", Duration::fromsecs(60), || self.db.bigdata()).await }
pub async fn get_other_data(&self) -> OtherType {
self.cache.get_or_else_update("other_data", Duration::from_secs(60), || self.db.other_data()).await
}
} ```
More sophisticated alternative: