Skip to content

Commit 43eaaff

Browse files
starknet_patricia_storage: dont cache reads in cached storage
1 parent 64c4d1f commit 43eaaff

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

crates/starknet_patricia_storage/src/map_storage.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ impl Storage for MapStorage {
7171
}
7272

7373
/// A storage wrapper that adds an LRU cache to an underlying storage.
74-
/// Only getter methods are cached.
74+
/// Getter methods are not cached.
7575
pub struct CachedStorage<S: Storage> {
7676
pub storage: S,
7777
pub cache: LruCache<DbKey, Option<DbValue>>,
@@ -223,12 +223,11 @@ impl<S: Storage> Storage for CachedStorage<S> {
223223
type Config = CachedStorageConfig<S::Config>;
224224

225225
async fn get(&mut self, key: &DbKey) -> PatriciaStorageResult<Option<DbValue>> {
226-
if let Some(cached_value) = self.cache.get(key) {
226+
if let Some(cached_value) = self.cache.peek(key) {
227227
return Ok(cached_value.clone());
228228
}
229229

230230
let storage_value = self.storage.get(key).await?;
231-
self.cache.put(key.clone(), storage_value.clone());
232231
Ok(storage_value)
233232
}
234233

@@ -245,7 +244,7 @@ impl<S: Storage> Storage for CachedStorage<S> {
245244
let mut indices_to_fetch = Vec::new();
246245

247246
for (index, key) in keys.iter().enumerate() {
248-
if let Some(cached_value) = self.cache.get(key) {
247+
if let Some(cached_value) = self.cache.peek(key) {
249248
values[index] = cached_value.clone();
250249
} else {
251250
keys_to_fetch.push(*key);
@@ -254,12 +253,9 @@ impl<S: Storage> Storage for CachedStorage<S> {
254253
}
255254

256255
let fetched_values = self.storage.mget(keys_to_fetch.as_slice()).await?;
257-
indices_to_fetch.iter().zip(keys_to_fetch).zip(fetched_values).for_each(
258-
|((index, key), value)| {
259-
self.cache.put((*key).clone(), value.clone());
260-
values[*index] = value;
261-
},
262-
);
256+
indices_to_fetch.iter().zip(fetched_values).for_each(|(index, value)| {
257+
values[*index] = value;
258+
});
263259

264260
Ok(values)
265261
}

0 commit comments

Comments
 (0)