crude_cache/
lib.rs

1#![deny(clippy::all)]
2
3//! # `CrudeCache`
4//!
5//! A simple utility for good enough concurrent caching needs.
6//!
7//! ## Overview
8//!
9//! `CrudeCache` uses a sharding approach to improve concurrent access. It divides the cached data across multiple internal `HashMap`s to reduce contention. Each shard is protected by its own `tokio::sync::RwLock`, ensuring safe concurrent read and write operations.
10//!
11//! For an example usage in application dataservice, check in [tests/cache_test.rs](https://github.com/defic/crude_cache/blob/master/tests/cache_test.rs).
12//! 
13//! ## Basic Usage
14//!
15//! ```rust
16//! use std::time::Duration;
17//! use crude_cache::CrudeCache;
18//!
19//! #[tokio::test]
20//! async fn example_test() {
21//!     let get_slow_data = || async {
22//!         tokio::time::sleep(Duration::from_secs(5)).await;
23//!         "test data"
24//!     };
25//!
26//!     let cache = CrudeCache::default();
27//!     //takes 5secs
28//!     let big_data = cache.get_or_else_update("cachekey", Duration::from_secs(60), get_slow_data).await;
29//!     //gets the cached data
30//!     let big_data = cache.get_or_else_update("cachekey", Duration::from_secs(60), get_slow_data).await;
31//! }
32//! ```
33//!
34//! ## Disclaimer
35//!
36//! Please note that `CrudeCache` was developed primarily for personal projects and has not been battle-tested in large-scale production environments. Contributions, suggestions, and feedback are always welcome!
37//!
38
39mod sharded_map;
40mod crude_cache;
41
42pub use crate::crude_cache::CrudeCache;