Git Product home page Git Product logo

Comments (7)

jaemk avatar jaemk commented on July 30, 2024

Yeah, that's currently the best way - using bool and true might be more obvious to read though. We could probably add an arg to cached to have it set bool and true automatically for you. I'm not sure what the best way to express that would be...

#[cached(time = 5, key = None)]
#[cached(time = 5, no_key)]
#[cached(time = 5, keyless)]
#[cached(time = 5, always)]

from cached.

EvanCarroll avatar EvanCarroll commented on July 30, 2024

Not sure of the right syntax @jaemk but I tried

#[cached(time = 5, key = "bool", convert = r#"{ true }"#)]

I'm getting a lot of errors with this though, I'm guessing this doesn't really ignore the arguments? They still have to have Hash?

error[E0308]: mismatched types
  --> src/main.rs:11:1
   |
11 | #[cached(time = 5, key = "bool", convert = r#"{ true }"#)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found reference
   |
   = note:   expected enum `std::result::Result<Vec<Alert>, reqwest::Error>`
           found reference `&std::result::Result<Vec<Alert>, reqwest::Error>`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `clone` found for enum `std::result::Result<Vec<Alert>, reqwest::Error>` in the current scope
   --> src/main.rs:11:1
    |
11  |   #[cached(time = 5, key = "bool", convert = r#"{ true }"#)]
    |   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method not found in `std::result::Result<Vec<Alert>, reqwest::Error>`
    | 
   ::: /home/ecarroll/.cargo/registry/src/github.com-1ecc6299db9ec823/reqwest-0.11.1/src/error.rs:12:1
    |
12  |   pub struct Error {
    |   ---------------- doesn't satisfy `reqwest::Error: Clone`
    |
    = note: the method `clone` exists but the following trait bounds were not satisfied:
            `Vec<Alert>: Clone`
            which is required by `std::result::Result<Vec<Alert>, reqwest::Error>: Clone`
            `reqwest::Error: Clone`
            which is required by `std::result::Result<Vec<Alert>, reqwest::Error>: Clone`
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

from cached.

jaemk avatar jaemk commented on July 30, 2024

I believe the error you're getting is because you need to add a result = true. The following

#[cached(time = 5, key = "bool", convert = r#"{ true }"#, result = true)]
async fn cached_test(s: String) -> std::result::Result<Vec<String>, &'static dyn std::error::Error> {
    Ok(vec![s])
}


println!("{:?}", cached_test("a".to_string()).await);
println!("{:?}", cached_test("definitely not a".to_string()).await);

prints out

Ok(["a"])
Ok(["a"])

from cached.

macthestack avatar macthestack commented on July 30, 2024

I actually tried #[cached(time = 5, key = None)] at first because it felt intuitive.

However, I'm starting to think that this is just an edge case of "I want to ingore this parameters" or "I just care about these parameters" which is already covered by key+convert. Maybe a more ergonomic addition would be to be able to select keys by name like:

#[cached(time = 5, keys = (a, b) )]
async fn something(db_pool: DBPool, a: String, b: String) -> Result<(), Error> {

I am not sure if that's how to do it in macro-land but it would be crystal clear to me as a user of the library. If there's a problem with mixing types I would certainly be ok with having to impl something like Hash on all keys so we get a tuple that's Hash (or a Unit in my no key case).

Anyway, thanks for a great lib!

from cached.

jaemk avatar jaemk commented on July 30, 2024

The tough part is being able to name the type that's the cache key, so while it's not as pretty as what you have there, I think this would work. Also the proc macro is using this crate https://docs.rs/darling/0.12.2/darling/ to parse the proc macro parameters, and one of the limitations is that the arguments need to be strings

#[cached(time = 5, key = "(String, String)", convert = r#"{ (a, b) }"#, result = true)]
async fn cached_test(a: String, b: String) -> std::result::Result<Vec<String>, &'static dyn std::error::Error> {
    Ok(vec![a])
}

from cached.

gitmalong avatar gitmalong commented on July 30, 2024

I believe the error you're getting is because you need to add a result = true. The following

#[cached(time = 5, key = "bool", convert = r#"{ true }"#, result = true)]
async fn cached_test(s: String) -> std::result::Result<Vec<String>, &'static dyn std::error::Error> {
    Ok(vec![s])
}


println!("{:?}", cached_test("a".to_string()).await);
println!("{:?}", cached_test("definitely not a".to_string()).await);

prints out

Ok(["a"])
Ok(["a"])

That gives me associated `static` items are not allowedrustc Any ideas?

from cached.

jaemk avatar jaemk commented on July 30, 2024

The #[once] macro was added back in October which I believe will do what you were asking for:

cached/tests/cached.rs

Lines 588 to 594 in e24d7a7

/// should only cache the _first_ value returned for 1 second.
/// all arguments are ignored for subsequent calls until the
/// cache expires after a second.
#[once(time = 1)]
fn only_cached_once_per_second(s: String) -> Vec<String> {
vec![s]
}

The arguments are passed through to your logic, but there is no "key" - it's only caching a singleton associated with the function.

from cached.

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.