make it impossible to fill up an unbounded amount of memory with caches
yeah storing Nones there is probably not a great idea isn't it
This commit is contained in:
parent
c87662419a
commit
645ae598f2
|
@ -81,7 +81,7 @@ pub struct DirEntry {
|
||||||
pub path: VPathBuf,
|
pub path: VPathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct ImageSize {
|
pub struct ImageSize {
|
||||||
pub width: u32,
|
pub width: u32,
|
||||||
pub height: u32,
|
pub height: u32,
|
||||||
|
|
|
@ -11,7 +11,7 @@ use super::{walk_dir_rec, Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf};
|
||||||
|
|
||||||
pub struct ContentCache<T> {
|
pub struct ContentCache<T> {
|
||||||
inner: T,
|
inner: T,
|
||||||
cache: DashMap<VPathBuf, Option<Vec<u8>>>,
|
cache: DashMap<VPathBuf, Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ContentCache<T> {
|
impl<T> ContentCache<T> {
|
||||||
|
@ -48,10 +48,13 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content(&self, path: &VPath) -> Option<Vec<u8>> {
|
fn content(&self, path: &VPath) -> Option<Vec<u8>> {
|
||||||
self.cache
|
self.cache.get(path).map(|x| x.clone()).or_else(|| {
|
||||||
.entry(path.to_owned())
|
let content = self.inner.content(path);
|
||||||
.or_insert_with(|| self.inner.content(path))
|
if let Some(content) = &content {
|
||||||
.clone()
|
self.cache.insert(path.to_owned(), content.clone());
|
||||||
|
}
|
||||||
|
content
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content_version(&self, path: &VPath) -> Option<String> {
|
fn content_version(&self, path: &VPath) -> Option<String> {
|
||||||
|
|
|
@ -6,7 +6,7 @@ use super::{Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf};
|
||||||
|
|
||||||
pub struct Blake3ContentVersionCache<T> {
|
pub struct Blake3ContentVersionCache<T> {
|
||||||
inner: T,
|
inner: T,
|
||||||
cache: DashMap<VPathBuf, Option<String>>,
|
cache: DashMap<VPathBuf, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Blake3ContentVersionCache<T> {
|
impl<T> Blake3ContentVersionCache<T> {
|
||||||
|
@ -31,15 +31,16 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn content_version(&self, path: &VPath) -> Option<String> {
|
fn content_version(&self, path: &VPath) -> Option<String> {
|
||||||
self.cache
|
self.cache.get(path).map(|x| x.clone()).or_else(|| {
|
||||||
.entry(path.to_owned())
|
let version = self.inner.content(path).map(|content| {
|
||||||
.or_insert_with(|| {
|
|
||||||
self.content(path).map(|content| {
|
|
||||||
let hash = blake3::hash(&content).to_hex();
|
let hash = blake3::hash(&content).to_hex();
|
||||||
format!("b3-{}", &hash[0..8])
|
format!("b3-{}", &hash[0..8])
|
||||||
|
});
|
||||||
|
if let Some(version) = &version {
|
||||||
|
self.cache.insert(path.to_owned(), version.clone());
|
||||||
|
}
|
||||||
|
version
|
||||||
})
|
})
|
||||||
})
|
|
||||||
.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn image_size(&self, path: &VPath) -> Option<ImageSize> {
|
fn image_size(&self, path: &VPath) -> Option<ImageSize> {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use super::{Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf};
|
||||||
|
|
||||||
pub struct ImageSizeCache<T> {
|
pub struct ImageSizeCache<T> {
|
||||||
inner: T,
|
inner: T,
|
||||||
cache: DashMap<VPathBuf, Option<ImageSize>>,
|
cache: DashMap<VPathBuf, ImageSize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ImageSizeCache<T> {
|
impl<T> ImageSizeCache<T> {
|
||||||
|
@ -59,15 +59,17 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn image_size(&self, path: &VPath) -> Option<ImageSize> {
|
fn image_size(&self, path: &VPath) -> Option<ImageSize> {
|
||||||
self.cache
|
self.cache.get(path).map(|x| *x).or_else(|| {
|
||||||
.entry(path.to_owned())
|
let image_size = self
|
||||||
.or_insert_with(|| {
|
.compute_image_size(path)
|
||||||
self.compute_image_size(path)
|
|
||||||
.inspect_err(|err| warn!("compute_image_size({path}) failed: {err:?}"))
|
.inspect_err(|err| warn!("compute_image_size({path}) failed: {err:?}"))
|
||||||
.ok()
|
.ok()
|
||||||
.flatten()
|
.flatten();
|
||||||
|
if let Some(image_size) = image_size {
|
||||||
|
self.cache.insert(path.to_owned(), image_size);
|
||||||
|
}
|
||||||
|
image_size
|
||||||
})
|
})
|
||||||
.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn anchor(&self, path: &VPath) -> Option<VPathBuf> {
|
fn anchor(&self, path: &VPath) -> Option<VPathBuf> {
|
||||||
|
|
Loading…
Reference in a new issue