Closed doivosevic closed 3 years ago
I think this error was hiding an underlying type error. The correct code which seems to work is this:
pub struct MyCache<V> {
pub dir: String,
my_hash: HashMap<Key, V>,
}
impl<V> MyCache<V> {
pub fn new() -> MyCache<V> {
MyCache {
dir: "./my_cache".to_string(),
my_hash: HashMap::new(),
}
}
fn get_key_hash(&self, key: &Key) -> String {
format!("{}__{}", key.0, key.1)
}
}
// This can be more generalized if we want to
type Key = (String, String);
impl<V: DeserializeOwned + Serialize> Cached<Key, V> for MyCache<V> {
fn cache_get(&mut self, k: &Key) -> Option<&V> {
let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;
let as_str = String::from_utf8(output).unwrap();
let o: V = serde_json::from_str(&as_str).unwrap();
self.my_hash.insert(k.to_owned(), o);
self.my_hash.get(k)
}
fn cache_set(&mut self, k: Key, v: V) -> Option<V> {
let vec = serde_json::to_vec(&v).unwrap();
cacache::write_sync(&self.dir, self.get_key_hash(&k), vec).unwrap();
Some(v)
}
fn cache_get_mut(&mut self, k: &Key) -> Option<&mut V> {
let output = cacache::read_sync(&self.dir, self.get_key_hash(k)).ok()?;
let as_str = String::from_utf8(output).unwrap();
let o: V = serde_json::from_str(&as_str).unwrap();
self.my_hash.insert(k.to_owned(), o);
self.my_hash.get_mut(k)
}
fn cache_get_or_set_with<F: FnOnce() -> V>(&mut self, k: Key, f: F) -> &mut V {
let gotten = self.cache_get(&k);
if gotten.is_none() {
let new = f();
self.cache_set(k.clone(), new).unwrap();
}
self.cache_get_mut(&k).unwrap()
}
fn cache_remove(&mut self, k: &Key) -> Option<V> {
let key = self.get_key_hash(k);
cacache::remove_sync(&self.dir, key).unwrap();
self.my_hash.remove(k)
}
fn cache_clear(&mut self) {
cacache::clear_sync(&self.dir).unwrap();
self.my_hash.clear();
}
fn cache_reset(&mut self) {
cacache::clear_sync(&self.dir).unwrap();
self.my_hash = HashMap::new();
}
fn cache_size(&self) -> usize {
self.my_hash.len()
}
}
I'm trying to implement a file based cacher. This is my cache type MyCache. I'm trying to follow readme.
When trying to create a cached function like this:
I get