Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ wasmparser = { workspace = true }
redb = { workspace = true }
directories = { workspace = true }
flate2 = { workspace = true }
dashmap = "6"

# Logging
tracing = { workspace = true }
Expand Down
55 changes: 55 additions & 0 deletions crates/core/src/cache/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use dashmap::DashMap;
use std::hash::Hash;
use std::sync::Arc;

pub trait CacheProvider<K, V> {
fn get(&self, key: &K) -> Option<Arc<V>>;
fn insert(&self, key: K, value: V);
fn remove(&self, key: &K) -> Option<Arc<V>>;
fn clear(&self);
}

pub struct MemoryCache<K, V> {
store: DashMap<K, Arc<V>>,
}

impl<K, V> MemoryCache<K, V>
where
K: Eq + Hash + Clone,
{
pub fn new() -> Self {
Self {
store: DashMap::new(),
}
}
}

impl<K, V> Default for MemoryCache<K, V>
where
K: Eq + Hash + Clone,
{
fn default() -> Self {
Self::new()
}
}

impl<K, V> CacheProvider<K, V> for MemoryCache<K, V>
where
K: Eq + Hash + Clone,
{
fn get(&self, key: &K) -> Option<Arc<V>> {
self.store.get(key).map(|v| Arc::clone(v.value()))
}

fn insert(&self, key: K, value: V) {
self.store.insert(key, Arc::new(value));
}

fn remove(&self, key: &K) -> Option<Arc<V>> {
self.store.remove(key).map(|(_, v)| v)
}

fn clear(&self) {
self.store.clear();
}
}
1 change: 1 addition & 0 deletions crates/core/src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod memory;
pub mod store;
Loading