feat: resettable cache and icon retry after time

This commit is contained in:
Michael Aaron Murphy 2025-04-02 17:21:11 +02:00
parent a28483f3d1
commit 09a76900a6
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
5 changed files with 63 additions and 17 deletions

View file

@ -1,9 +1,10 @@
use once_cell::sync::Lazy;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::sync::Mutex;
use std::time::Instant;
pub(crate) static CACHE: Lazy<Cache> = Lazy::new(Cache::default);
pub(crate) static CACHE: LazyLock<Cache> = LazyLock::new(Cache::default);
type IconMap = BTreeMap<(String, u16, u16), CacheEntry>;
type ThemeMap = BTreeMap<String, IconMap>;
@ -13,7 +14,7 @@ pub(crate) struct Cache(Mutex<ThemeMap>);
#[derive(Debug, Clone, PartialEq)]
pub enum CacheEntry {
// We already looked for this and nothing was found, indicates we should not try to perform a lookup.
NotFound,
NotFound(Instant),
// We have this entry.
Found(PathBuf),
// We don't know this entry yet, indicate we should perform a lookup.
@ -21,6 +22,10 @@ pub enum CacheEntry {
}
impl Cache {
pub fn clear(&self) {
self.0.lock().unwrap().clear();
}
pub fn insert<P: AsRef<Path>>(
&self,
theme: &str,
@ -33,7 +38,7 @@ impl Cache {
let entry = icon_path
.as_ref()
.map(|path| CacheEntry::Found(path.as_ref().to_path_buf()))
.unwrap_or(CacheEntry::NotFound);
.unwrap_or(CacheEntry::NotFound(Instant::now()));
match theme_map.get_mut(theme) {
Some(icon_map) => {
@ -56,4 +61,16 @@ impl Cache {
.and_then(|path| path.cloned())
.unwrap_or(CacheEntry::Unknown)
}
pub fn reset_none(&self) {
let mut theme_map = self.0.lock().unwrap();
for (_theme_name, theme) in theme_map.iter_mut() {
for (_icon_data, cached_icon) in theme.iter_mut() {
if matches!(cached_icon, CacheEntry::NotFound(_)) {
*cached_icon = CacheEntry::Unknown;
}
}
}
}
}