Giter Club home page Giter Club logo

Comments (4)

ben-manes avatar ben-manes commented on July 4, 2024 1

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.

lilyball avatar lilyball commented on July 4, 2024

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.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024

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.

tatsuya6502 avatar tatsuya6502 commented on July 4, 2024

I am working on this for v0.10.0 release via #193.

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.