Git Product home page Git Product logo

Comments (1)

doivosevic avatar doivosevic commented on June 28, 2024

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()
    }
}

from cached.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.