https://github.com/boundless-forest/rust-cache
An in-memory key-value timeout cache library for Rust
https://github.com/boundless-forest/rust-cache
cache in-memeory rust
Last synced: about 1 year ago
JSON representation
An in-memory key-value timeout cache library for Rust
- Host: GitHub
- URL: https://github.com/boundless-forest/rust-cache
- Owner: boundless-forest
- License: apache-2.0
- Created: 2020-08-24T08:20:37.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-21T13:32:32.000Z (over 5 years ago)
- Last Synced: 2025-01-28T03:19:52.593Z (over 1 year ago)
- Topics: cache, in-memeory, rust
- Language: Rust
- Homepage:
- Size: 26.4 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# *rust-cache*
rust-cache is an in-memory key-value cache library for rust. It's safe in multi-threaded scenarios and you can set value with specific expiration time, `DEFAULT_EXPIRATION` or `NO_EXPIRATION`.
## Build
```sh
$ cargo build
```
## Usage
```rs
// set.rs
use rust_cache::cache::new;
use rust_cache::cache::{DEFAULT_EXPIRATION, NO_EXPIRATION};
use std::thread;
use std::time::Duration;
fn main() {
let (mut tc, _) = new(Duration::from_secs(50), Duration::from_secs(1));
tc.set("a", serde_json::json!(1), DEFAULT_EXPIRATION);
tc.set("b", serde_json::json!("2"), NO_EXPIRATION);
tc.set("c", serde_json::json!(true), Duration::from_secs(20));
thread::sleep(Duration::from_secs(25));
assert_eq!(tc.get("a").unwrap(), 1);
assert_eq!(tc.get("b").unwrap(), "2");
assert!(tc.get("c").is_none());
thread::sleep(Duration::from_secs(30));
assert!(tc.get("a").is_none());
assert_eq!(tc.get("b").unwrap(), "2");
}
```
## Run Example
```sh
$ cargo run --example set
```