[pbs-devel] [PATCH proxmox-backup 1/2] tools: lru cache: allow to dynamically increase the cache capacity
Christian Ebner
c.ebner at proxmox.com
Fri Aug 1 16:10:23 CEST 2025
Currently, the capacity of the LRU cache is set at instantiation, not
allowing to change it afterwards. In some situations, e.g. when more
space/memory is available, dynamically increasing it is required.
Therefore, add the methods which allow to increase the capacity by a
given increment, for the lru cache, async lru cache and the local
datastore lru caches, respectively.
Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
pbs-datastore/src/local_datastore_lru_cache.rs | 7 +++++++
pbs-tools/src/async_lru_cache.rs | 8 ++++++++
pbs-tools/src/lru_cache.rs | 6 ++++++
3 files changed, 21 insertions(+)
diff --git a/pbs-datastore/src/local_datastore_lru_cache.rs b/pbs-datastore/src/local_datastore_lru_cache.rs
index c0edd3619..00cce94d6 100644
--- a/pbs-datastore/src/local_datastore_lru_cache.rs
+++ b/pbs-datastore/src/local_datastore_lru_cache.rs
@@ -177,4 +177,11 @@ impl LocalDatastoreLruCache {
pub fn contains(&self, digest: &[u8; 32]) -> bool {
self.cache.contains(*digest)
}
+
+ /// Increases the capacity of the cache by given increment.
+ ///
+ /// Returns the new cache capacity.
+ pub fn increase_capacity(&self, increment: usize) -> usize {
+ self.cache.increase_capacity(increment)
+ }
}
diff --git a/pbs-tools/src/async_lru_cache.rs b/pbs-tools/src/async_lru_cache.rs
index 3a975de32..dbfa1e21d 100644
--- a/pbs-tools/src/async_lru_cache.rs
+++ b/pbs-tools/src/async_lru_cache.rs
@@ -39,6 +39,14 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V: Clone + Send + 'static> AsyncL
}
}
+ /// Increment the LRU cache capacity by given increment, saturating at MAX value for usize
+ ///
+ /// Returns the new capacity.
+ pub fn increase_capacity(&self, increment: usize) -> usize {
+ let mut maps = self.maps.lock().unwrap();
+ maps.0.increase_capacity(increment)
+ }
+
/// Access an item either via the cache or by calling cacher.fetch. A return value of Ok(None)
/// means the item requested has no representation, Err(_) means a call to fetch() failed,
/// regardless of whether it was initiated by this call or a previous one.
diff --git a/pbs-tools/src/lru_cache.rs b/pbs-tools/src/lru_cache.rs
index a7aea6528..d12d0675e 100644
--- a/pbs-tools/src/lru_cache.rs
+++ b/pbs-tools/src/lru_cache.rs
@@ -133,6 +133,12 @@ impl<K: std::cmp::Eq + std::hash::Hash + Copy, V> LruCache<K, V> {
}
}
+ /// Increments the cache capacity by given `increment`, saturating at MAX value for usize
+ pub fn increase_capacity(&mut self, increment: usize) -> usize {
+ self.capacity = self.capacity.saturating_add(increment);
+ self.capacity
+ }
+
/// Insert or update an entry identified by `key` with the given `value`.
/// This entry is placed as the most recently used node at the head.
pub fn insert<F>(&mut self, key: K, value: V, removed: F) -> Result<bool, anyhow::Error>
--
2.47.2
More information about the pbs-devel
mailing list