Comments (10)
Hi @tatsuya6502 ,
Thanks for looking into it.
The "keys() -> Vec" would work if it is easier...
JR
from moka.
@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.
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.
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.
- Each cache has 64 segments (hard-coded since Moka v0.1.0), except
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.
@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.
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):
Line 1455 in 52c9f55
or this (IntoIterator
):
Lines 1491 to 1495 in 52c9f55
from moka.
Sweet...
from moka.
@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.
@tatsuya6502 ..
Super awesome..
JR
from moka.
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)
- Add new api similar to `try_get_with`, but return `Option` rather than `Result`? HOT 6
- Is it worth to modify the key of `get_with` as a `&K` rather than `K`? HOT 10
- Help with size based eviction that weighs with bytes HOT 2
- moka is too polymorphic (cargo-llvm-lines) HOT 3
- Setting number of segments will disable notifications HOT 2
- try_get_with always runs init future HOT 4
- Staggering expiry with TTL HOT 1
- Moka `get_with` inflates future size by ~7x HOT 14
- how to use build_with_hash? HOT 3
- Clarify is_fresh() with regard to concurrent insert_with() HOT 3
- Entry `and_compute` method for performing an insert, update, or remove operation with single closure HOT 9
- Add support for `Immediate` notification delivery mode to `future` cache HOT 1
- [help] Conditionally disabling the cache, and logging when data retrieved from cache HOT 8
- Cache with max capacity of zero should not cache anything
- Proposal: support insert key with expire time? HOT 16
- CI: Disable external CI services on temporary merge-queue branches?
- Statistics for metrics HOT 3
- `get_with` deadlock HOT 13
- `.blocking().invalidate(key)` doesn't trigger eviction listener HOT 1
- RUSTSEC-2020-0168: mach is unmaintained HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moka.