extern crate simple_disk_cache;
extern crate tempdir;
use simple_disk_cache::SimpleCache;
use simple_disk_cache::config::{CacheConfig, CacheStrategy, DataEncoding};
use tempdir::TempDir;
type TestCache = SimpleCache<u32, u64>;
fn get_tempdir(prefix: &'static str) -> TempDir {
TempDir::new(format!("sdc_test_{}", prefix).as_str()).expect("failed setting up temp directory")
}
#[test]
fn basic_usage() {
let tempdir = get_tempdir("basic_usage");
let config = CacheConfig {
max_bytes: 10 * 1024 * 1024,
encoding: DataEncoding::Json,
strategy: CacheStrategy::LRU,
subdirs_per_level: 3,
};
let mut cache =
TestCache::initialize(tempdir.as_ref(), config).expect("failed initializing cache.");
for k in 5..41 {
let v = (k * 2) as u64;
cache.put(&k, &v).expect("failed writing to cache.");
}
for k in 5..41 {
let v_expected = (k * 2) as u64;
let v = cache.get(&k).expect("failed reading from cache.");
assert_eq!(v, Some(v_expected));
}
}
#[test]
fn restore_cache() {
let tempdir = get_tempdir("restore_cache");
let config1 = CacheConfig {
max_bytes: 10 * 1024 * 1024,
encoding: DataEncoding::Json,
strategy: CacheStrategy::LRU,
subdirs_per_level: 3,
};
let config2 = config1.clone();
let mut cache =
TestCache::initialize(tempdir.as_ref(), config1).expect("failed initializing cache.");
for k in 5..41 {
let v = (k * 2) as u64;
cache.put(&k, &v).expect("failed writing to cache.");
}
drop(cache);
let mut cache =
TestCache::initialize(tempdir.as_ref(), config2).expect("failed initializing cache.");
for k in 5..41 {
let v_expected = (k * 2) as u64;
let v = cache.get(&k).expect("failed reading from cache.");
assert_eq!(v, Some(v_expected));
}
}