oxedyne/fe2o3/fe2o3_o3db_sync/src/file/fcache.rs
1.5 KiB, 1 run
created by r1870400018:791, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::{ |
| 2 | prelude::*, |
| 3 | file::floc::FileNum, |
| 4 | }; |
| 5 | |
| 6 | use std::{ |
| 7 | collections::BTreeMap, |
| 8 | fs::File, |
| 9 | sync::{ |
| 10 | Arc, |
| 11 | RwLock, |
| 12 | }, |
| 13 | time::{ |
| 14 | Duration, |
| 15 | Instant, |
| 16 | }, |
| 17 | }; |
| 18 | |
| 19 | #[derive(Debug)] |
| 20 | pub struct FileCacheEntry { |
| 21 | pub t: Instant, |
| 22 | pub file: Arc<RwLock<File>>, |
| 23 | } |
| 24 | |
| 25 | #[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 26 | pub struct FileCacheIndex { |
| 27 | pub fnum: FileNum, |
| 28 | pub typ: FileType, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, Default)] |
| 32 | pub struct FileCache { |
| 33 | map: BTreeMap<FileCacheIndex, FileCacheEntry>, |
| 34 | exp: Duration, // expiry duration |
| 35 | } |
| 36 | |
| 37 | impl FileCache { |
| 38 | pub fn new(expiry_secs: u64) -> Self { |
| 39 | Self { |
| 40 | map: BTreeMap::new(), |
| 41 | exp: Duration::from_secs(expiry_secs), |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn ref_map(&self) -> &BTreeMap<FileCacheIndex, FileCacheEntry> { &self.map } |
| 46 | pub fn mut_map(&mut self) -> &mut BTreeMap<FileCacheIndex, FileCacheEntry> { &mut self.map } |
| 47 | pub fn expiry(&self) -> &Duration { &self.exp } |
| 48 | pub fn len(&self) -> usize { self.map.len() } |
| 49 | |
| 50 | pub fn insert( |
| 51 | &mut self, |
| 52 | fnum: FileNum, |
| 53 | typ: &FileType, |
| 54 | file: Arc<RwLock<File>>, |
| 55 | ) { |
| 56 | self.map.insert( |
| 57 | FileCacheIndex{ fnum, typ: typ.clone() }, |
| 58 | FileCacheEntry { |
| 59 | t: Instant::now(), |
| 60 | file: file, |
| 61 | }, |
| 62 | ); |
| 63 | } |
| 64 | } |