Crates.io | remember-this |
lib.rs | remember-this |
version | 0.1.0 |
source | src |
created_at | 2020-11-27 08:17:11.679903 |
updated_at | 2020-11-27 08:17:11.679903 |
description | A simple mechanism for caching data to both memory and disk. Uses `tokio`. |
homepage | |
repository | https://github.com/matrix-org/disk-cache-rs |
max_upload_size | |
id | 316977 |
size | 41,331 |
This crate provides a simple, zero-configuration, mechanism for caching data both in-memory and to disk.
A typical usecase is an application that needs to deal with long-running queries to the web or to a database. If the application fails for some reason or if it is a command-line application, the cache will let the application resume its next run from where it stopped.
extern crate remember_this;
#[macro_use]
extern crate tokio;
use remember_this::*;
#[tokio::main]
async fn main() {
// A cache manager lets us organize several strongly-typed caches.
let manager_options = CacheManagerOptions::builder()
.path("/tmp/test_reopen.db")
.build();
let manager = CacheManager::new(&manager_options).unwrap();
// An individual cache is strongly-typed.
//
// With the default policy, items remain 1h in memory and 1d on disk,
// this should be sufficient for our test.
let cache_options = CacheOptions::default();
let cache = manager.cache("my_cache", &cache_options).unwrap();
// Fill the cache. Imagine that it's actually a long computation.
//
// Recall that async blocks are computed lazily in Rust, so `async { i * i }`
// will only execute if it's not in the cache yet.
for i in 0..100 {
let obtained = cache.get_or_insert_infallible(&i, async { i * i }).await.unwrap();
assert_eq!(*obtained, i * i);
}
// Let's refetch from the cache.
for i in 0..100 {
// Here, the async blocks are actually not executed.
let obtained = cache.get_or_insert_infallible(&i, async { panic!("We shouldn't reach this point"); }).await.unwrap();
assert_eq!(*obtained, i * i);
}
}