Giter Club home page Giter Club logo

Comments (3)

lilyball avatar lilyball commented on July 23, 2024 1

If it's easy enough to show those and won't be too expensive then that sounds fine to me. It just bothered me that my type that has a cache couldn't show anything at all about it.

It looks like #134 is adding methods to query for these stats, which I appreciate as it means I have the option of showing those after all if it suits my usage better.

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 23, 2024

Thank you for reporting this issue.

I'm not sure what offhand such an impl should show, but perhaps some basic stats about the cache if that's easy to gather.

I started to work on this as #134 and made the Debug impl to show some basic stats. But while I was writing a code sample for the doc, I realized this will confuse users as these stats are eventual consistent (will show stale numbers).

use moka::sync::Cache;let cache = Cache::new(10);// Insert entries.
cache.insert('n', "Netherland Dwarf");
cache.insert('l', "Lop Eared");
​cache.insert('d', "Dutch");

// Ensure an entry exists.
assert_eq!(cache.get(&'l'), Some("Lop Eared"));// Print a debug info. Note that this may print stale numbers.
// (e.g. `entry_count` can be `0` instead of `3`)
println!("{:?}", cache);
// -> Cache { max_capacity: Some(10), entry_count: 0, weighted_size: 0 }// You can mitigate this by performing a `sync` first.
// Bring `ConcurrentCacheExt` trait to the scope so we can use `sync` method.
use moka::sync::ConcurrentCacheExt;
// Call `sync` to run pending internal tasks.
cache.sync();

// Now it will print the actual numbers.
println!("{:?}", cache);
// -> Cache { max_capacity: Some(10), entry_count: 3, weighted_size: 3 }

These are kept eventual consistent to improve performance (doc).

So I think I will change it to show key-value entries like the Debug impl of std::collections::HashMap does. It will be something like:

use moka::sync::Cache;let cache = Cache::new(10);// Insert entries.
cache.insert('n', "Netherland Dwarf");
cache.insert('l', "Lop Eared");
​cache.insert('d', "Dutch");

// Print a debug info.
println!("{:?}", cache);
// -> {'l': "Lop Eared", 'd': "Dutch", 'n': "Netherland Dwarf"}
// Note that entries are returned in arbitrary order.

This will never show stale values, so it will not confuse users.

To show key-value entries, both K and V must implement Debug. I hope you are okay with this.

from moka.

tatsuya6502 avatar tatsuya6502 commented on July 23, 2024

Hi @lilyball — I have published Moka v0.8.5 to crates.io containing this and some other changes. Thanks!

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.