Crates.io | rust_caching |
lib.rs | rust_caching |
version | 0.1.0 |
source | src |
created_at | 2021-08-07 16:28:03.520261 |
updated_at | 2021-08-07 16:28:03.520261 |
description | A simple safe Rust library to cache functions (or sections of code) in memory |
homepage | https://github.com/Cyclip/rust_caching |
repository | https://github.com/Cyclip/rust_caching |
max_upload_size | |
id | 432842 |
size | 10,253 |
A simple safe Rust library to cache functions (or sections of code) in memory, mainly ported from my Python module SimpleCache.
git clone https://github.com/Cyclip/rust_caching/
rust_caching
dependency into Cargo.toml
:[package]
name = "example"
version = "0.1.0"
edition = "2018"
[dependencies]
rust_caching = {path = "rust_caching"}
Here's a demo which caches an expensive function to calculate the number of possible steps to a specific stair; assuming you can only take up to 3:
extern crate rust_caching;
use rust_caching::*;
fn steps_to(cache: &mut MemCache, stair: u128) -> u128 {
// check the `cache` variable with input arguments `stair`,
// with an output type `u128`
check_cache!(cache, args!(stair), u128, {
// If it isn't cached, it will run this block of code
// and cache it afterwards automatically.
match stair {
1 => { 1 },
2 => { 2 },
3 => { 4 },
_ => {
steps_to(cache, stair - 3)
+ steps_to(cache, stair - 2)
+ steps_to(cache, stair - 1)
}
}})
}
fn main() {
// stair = 36 would take about 10 seconds
// as stair increases, the time taken expotentially increases
let mut cache = MemCache::new(100); // create a new memory cache struct
let stair = 120u128;
let result = steps_to(&mut cache, stair);
println!("Steps to {}: {}", stair, result);
println!("Cache size: {}", cache.size());
}
The args!
macro converts a group of arguments into a hashed ID which will then be compared with in the future.
The check_cache!
macro takes in the cache struct, input arguments, output type and a block of code to cache and handles it for you.
rust_caching is licensed with the MIT License and is created by Cyclip