more functional

This commit is contained in:
Nikolay Volf 2016-02-17 23:25:25 +03:00
parent 40f5ea4007
commit aad49cb19f

View File

@ -510,21 +510,18 @@ impl KeyDirectory {
cache_usage.drain(..untracked_usages);
}
let mut cache = self.cache.borrow_mut();
if cache.len() <= MAX_CACHE_USAGE_TRACK { return; }
if self.cache.borrow().len() <= MAX_CACHE_USAGE_TRACK { return; }
let uniqs: HashSet<&Uuid> = cache_usage.iter().collect();
let mut removes = HashSet::new();
for key in cache.keys() {
if !uniqs.contains(key) {
removes.insert(key.clone());
let removes: Vec<Uuid> = {
let cache = self.cache.borrow();
cache.keys().cloned().filter(|key| !uniqs.contains(key)).collect()
};
for key in removes {
self.cache.borrow_mut().remove(&key);
}
}
for removed_key in removes { cache.remove(&removed_key); }
}
/// Reports how many keys are currently cached.
pub fn cache_size(&self) -> usize {
self.cache.borrow().len()