Comments (4)
Ideally the caller doesn't need to know for business logic purposes and we shouldn't corrupt our apis if only for debug logging. The main reason would be for stats, which in Caffeine has a pluggable StatsCounter
for the cases it records. The APIs can't be perfect so the goal has to be low conceptual weight, correctness / maintainability, and allowing users to solve their core problems. If the latter has to be sometimes a bit of a dirty one-off, then that is a reasonable decision. (See bumper-sticker api design).
Caffeine has this same problem with the backing ConcurrentHashMap
and its compute methods. Similarly for some integrations into other's apis (most recently Coherence which requires additional callbacks). A mutable variable that the thread populates during a compute is very inexpensive, short-lived, and an isolated implementation detail. Given all the unique benefits these caches provide, the small code smell seems fine to me.
When it is not possible or a much worse solution than native support, I think stashing these ad hoc methods somewhere else (e.g. cache.policy()
) is a good tradeoff. Less discoverable but unblocks those who ask and need it, and an escape hatch from trying to have a universal api. While I wouldn't add it to Caffeine as not carrying its weight, each language / ecosystem is different and I am curious to see how @tatsuya6502 evolves his cache to gleem his insights.
from moka.
It occurs to me that the future given to get_with
doesn't have the 'static
bound so I probably can just stuff a boolean on the stack. That certainly makes it easier, it would just be more elegant if moka could tell me directly.
from moka.
It would be great if the cache could simply tell me whether it was fresh or cached.
Technically, this will be very easy to implement. get_with
internally tracks whether the value was fresh or cached (code), so it is easy to return such information as a part of the return value. For example, get_with
could return a tuple (V, bool)
where bool
indicates whether V
was fresh or not. Or, it could return a struct having value: V
and is_fresh: bool
as the fields.
The only problem is that we cannot change get_with
's return value from V
as it seems many users are already using it and happy with it. So (if we take this path) we will need to add another method that will work as get_with
but has a different return type. And then we will need to think about other methods: try_get_with
and get_with_if
. Do we need to do the same thing to them?
I am very open to adding such method(s) but I would like to keep the core Cache
API small. So we will probably do something that @ben-manes, the Caffeine author, suggested before (comment)?
(In Caffeine) A lot of random functionality, like conditional entry removal or policy inspection, is hidden away under nested objects (
asMap()
,policy()
) to provide the features while keeping the core interface simple to reduce the conceptual weight for learning a new library.
Perhaps, something like this?
// We will keep current `get_with` as is.
let val = cache.get_with(key, async {...}).await;
// For accessing advanced features, use a new `entry` method, which mimics `HashMap`'s `entry` method.
let result = cache.entry(key).or_insert_with(async {...}).await;
// `result` is a struct providing some methods to access its `bool` and `V` fields.
if result.is_fresh() {
info!("Fresh value");
}
let val = result.into_value(); // This consumes `self` and returns `V`.
If we can come up with a nice interface, I would also hide unpopular get_with_if
using it.
from moka.
I am working on this for v0.10.0 release via #193.
from moka.
Related Issues (20)
- CI: Enable Miri tests on `moka::cht::*` modules
- Memory corruption observed when using Moka v0.9.6 HOT 15
- unbounded capacity? HOT 2
- Possibility of using async runtime tasks instead of thread pools HOT 10
- oom caused after use #234's statistics record code HOT 4
- How can i add something to the cache inside the `eviction_listener`? HOT 5
- Moka loses cache with curl HOT 9
- Provide an easy way to implement per-entry TTL and TTI HOT 6
- Enable `clippy::arc_with_non_send_sync` lint for active branches
- Provide a way to iterate entries with their metadata
- Provide a way to get a read-only snapshot of the `FrequencySketch` of `Cache`
- Provide a way to restore a `Cache` from entries with metadata and a `FrequencySketch` snapshot HOT 2
- CI: Temporary disable CirrusCI HOT 3
- wasm compatibility - change Expiry to avoid needing std::time::Instant::now() HOT 4
- An internal `do_insert_with_hash` method gets the current `Instant` too early when eviction listener is enabled HOT 1
- Tracking issue for restoring cache state from backed up entries and a snapshot of the LFU filter
- Reason for `Arc<Error>` HOT 2
- Memory leak in moka 0.12 HOT 13
- With Rust 1.73.0, some unit tests started to fail for `mips-unknown-linux-musl` target HOT 7
- not support `armv5te-unknown-linux-musleabi` 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.