1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
//! In-Process In-Memory Cache implementation.
//!
//! Supported eviction strategy:
//! - LRU (Least Recently Used, inspired by [LRU Cache](https://github.com/jeromefroe/lru-rs))
//! - LFU (Least Frequently Used, TBD)
//! - MRU (Most Recently Used, TBD)
//! - FIFO (First In First Out, TBD)
//!
//! Supporting async/.await powered by tokio runtime.
//!
//! ## Caveat
//!
//! If you need to use non-asynchronous, just disable default feature for this crate on your `Cargo.toml`
//!
//! If you do need to use reference for your value, on non-asynchronous do use `std::rc::Rc`,
//! and on asynchronous do use `std::sync::Arc`
//!
//! ## Example basic
//!
//! ### Cargo.toml
//!
//! ```toml
//! [dependencies]
//! aba-cache = { version = "0.1", default-features = false }
//! serde_json = { version = "1.0" }
//! ```
//!
//! ### main.rs
//!
//! ```rust,no_run
//! use aba_cache as cache;
//! use cache::LruCache;
//! use serde_json::{self, Value};
//! use std::rc::Rc;
//!
//! // create cache with multiply_cap 1024, and evicting
//! // LRU entry with age 15 minutes or older
//! let mut cache = LruCache::new(1024, 15 * 60);
//!
//! let val_one: Rc<Value> = Rc::new(serde_json::from_str(r#"{"id":1}"#).unwrap());
//! let val_two: Rc<Value> = Rc::new(serde_json::from_str(r#"{"id":2}"#).unwrap());
//!
//! cache.put("one", val_one.clone());
//! cache.put("two", val_two.clone());
//!
//! assert!(
//!     if let Some(value) = cache.get(&"one") {
//!         *value == val_one
//!     } else {
//!         false
//!     }
//! );
//! assert_eq!(cache.get(&"three"), None);
//! ```
//!
//! ## Example async
//!
//! ### Cargo.toml
//!
//! ```toml
//! aba-cache = { version = "0.1", default-features = false }
//! serde_json = { version = "1.0" }
//! tokio = { version = "0.2", features = ["macros", "rt-core"] }
//! ```
//!
//! ### main.rs
//!
//! ```rust,no_run
//! use aba_cache as cache;
//! use cache::LruAsyncCache;
//! use serde_json::{self, Value};
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     // create cache with multiply_cap 1024, and evicting
//!     // LRU entry with age 15 minutes or older
//!     let cache = LruAsyncCache::new(1024, 15 * 60);
//!
//!     let val_one: Arc<Value> = Arc::new(serde_json::from_str(r#"{"id":1}"#)?);
//!     let val_two: Arc<Value> = Arc::new(serde_json::from_str(r#"{"id":2}"#)?);
//!
//!     cache.put("one", val_one.clone()).await;
//!     cache.put("two", val_two.clone()).await;
//!
//!     assert!(
//!         if let Some(value) = cache.get(&"one").await {
//!             value == val_one
//!         } else {
//!             false
//!         }
//!     );
//!     assert_eq!(cache.get(&"three").await, None);
//!
//!     Ok(())
//! }
//! ```
mod lru;

#[cfg(feature = "asynchronous")]
pub use lru::asynchronous::Cache as LruAsyncCache;
pub use lru::Cache as LruCache;