43 lines
1.1 KiB
Rust
43 lines
1.1 KiB
Rust
pub struct Cache {
|
|
// A limit on how many open files can be in this cache
|
|
// Usefull to prevent hundreds of processes caching thousands of files
|
|
// 0 == No Limit
|
|
file_id_limit: u64,
|
|
|
|
// Total bytes allowed to be cached before a file is pushed out of the cache
|
|
total_byte_size: u64,
|
|
current_byte_size: u64,
|
|
cache: Vec<(FileID, Vec<u8>)>,
|
|
}
|
|
|
|
impl Cache {
|
|
fn default() -> Self {
|
|
Self {
|
|
file_id_limit: 1024,
|
|
total_byte_size: 1024 * 16,
|
|
current_byte_size: 0,
|
|
cache: Vec::new(),
|
|
}
|
|
}
|
|
fn recalculate_cache(&mut self) {
|
|
let mut current: u64 = 0;
|
|
for (_, file) in &self.cache {
|
|
current += file.len() as u64;
|
|
}
|
|
self.current_byte_size = current;
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn recalc_cache_test() {
|
|
let mut cache = Cache::default();
|
|
let mut temp_file_vec: Vec<u8> = Vec::new();
|
|
for x in 0u64..=10 {
|
|
temp_file_vec.push(x.try_into().unwrap());
|
|
let file = (x, temp_file_vec.clone());
|
|
cache.cache.push(file);
|
|
}
|
|
cache.recalculate_cache();
|
|
assert_eq!(cache.current_byte_size, 66);
|
|
}
|