jaemk / cached

Rust cache structures and easy function memoization
MIT License
1.58k stars 95 forks source link

Need to serialize/deserialize the cache #49

Open Stargateur opened 4 years ago

Stargateur commented 4 years ago

I need to serialize the cache:

  1. There is currently no way to iterate with Cached trait
  2. Without GATs I don't think we could have an associate Iter type on Cached, maybe it's possible I don't known
  3. We could implement Serialize/Deserialize from serde for CachedSized & Co, this require some work and have some cons but this could allow to keep time(not very useful for timed cache) and order information.
  4. We could add a method in Cached to return an HashMap of Cache consuming self, not all implementation could do it without allocate a new hashmap and we lost information about time or order.
Stargateur commented 4 years ago

After testing around I think both could be nice to have.

jaemk commented 4 years ago

In order of my thoughts:

(3): I think we should avoid implementing Serialize/Deserialize since we'd have to maintain that contract between library versions and that'd be a major pain.

(2): I'm not sure either, but seems like it might be more complicated than it's worth

(1. & 4): I think we should add a .iter method to the Cached trait for returning all cached keys and values in an unspecified order (as if you were pulling the values from a hashmap), and then implement std::iter::FromIterator for each cache store. I think it's reasonable and okay to lose internal metadata (like time) when going to/from an iterator, and leave that as an implementation detail of each cache type, e.g. SizedCache returns an iterator in the order of oldest to newest and its FromIterator implementation assumes the same oldest to newest ordering so it can simply cache_set all the values in order.

Stargateur commented 4 years ago

I think we should add a .iter method to the Cached trait for returning all cached keys and values

Yes that my first though but that not easy as its look see https://github.com/rust-lang/rust/issues/44265. I don't think it's currently possible.