Giter Club home page Giter Club logo

Comments (10)

JRAndreassen avatar JRAndreassen commented on July 24, 2024 1

Hi @tatsuya6502 ,

Thanks for looking into it.
The "keys() -> Vec" would work if it is easier...
JR

from moka.

JRAndreassen avatar JRAndreassen commented on July 24, 2024 1

@tatsuya6502 ,
Excellent..
I'll be testing it here in the next few day's...
I'll let you know how it goes.

Thanks for fixing it...
JR

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 24, 2024

Hi. Currently (v0.8.1), moka::future::Cache does not provide a way to iterate through all entries or retrieve all keys in a cache. There is an experimental cache implementation called moka::dash::Cache that has iter() method, but it is not optimized for asynchronous use.

Let me spend a few days to explore some options to provide either iter() or keys() method.

Providing such a method in a concurrent collection will be much more complicated than in a not-thread-safe collection (e.g. std::collections::HashTable). This is because concurrent collections allow updates from other threads while some threads are iterating. So some experimentation will be needed.

I will get back to you when I decide what to do.

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 24, 2024

I believe I found a good solution for providing iterators. Although it has some disadvantages (below), I hope it will be good enough for many use cases:

Disadvantages:

  • It's next method may not (or may) return some entries (key-value pairs) if they have inserted after the iterator was created.
  • It has some memory overheads as it creates and holds a snapshot of keys (Vec<std::sync::Weak<K>>) of an internal hash table segment:
    • Each cache has 64 segments (hard-coded since Moka v0.1.0), except sync::SegmentedCache.
    • An iterator creates and hold one snapshot of only one segment at a time.
      • So this will consume less memory than creating/holding a snapshot of the entire hash table.

Advantages:

  • It does not reduce concurrency.
    • Allows concurrent reads and writes on the cache while iterators are alive.
  • Its implementation is simple and reliable.

I found implementing such an iterator requires about the same amount of work to implement keys() -> Vec method. Since the iterator is more commonly used in Rust libraries and it will consume 64 times smaller amount of memory than keys(), I took the iterator path (#114).

from moka.

JRAndreassen avatar JRAndreassen commented on July 24, 2024

@tatsuya6502 ,
Iterator works for me..
if it implements the 'core::iter::Iterator' trait, that would be awesome..

Thanks for looking into it...
JR

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 24, 2024

@JRAndreassen,

Yes. The iter method returns an iterator, which implements the core::iter::Iteranor trait. Also &Cache implements the core::iter::IntoIterator trait.

So you can either do this (iter method):

assert_eq!(cache.iter().count(), 1);

or this (IntoIterator):

moka/src/future/cache.rs

Lines 1491 to 1495 in 52c9f55

for (key, value) in &cache {
assert_eq!(value, make_value(*key));
key_set.insert(*key);
}

from moka.

JRAndreassen avatar JRAndreassen commented on July 24, 2024

Sweet...

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 24, 2024

@JRAndreassen — I am going to merge #114 with the iterator supports. Then, I will do some pre-release testing and release v0.8.2 in a day or so. I will let you know when v0.8.2 is released.

from moka.

JRAndreassen avatar JRAndreassen commented on July 24, 2024

@tatsuya6502 ..
Super awesome..
JR

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 24, 2024

Hi @JRAndreassen — OK. I have published Moka v0.8.2 to crates.io. Please let me know if you have any questions.

from moka.

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.