Comments (7)
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.
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.
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.
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.
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.
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.
The #[once]
macro was added back in October which I believe will do what you were asking for:
Lines 588 to 594 in e24d7a7
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)
- Cron-like cache clearing HOT 1
- Mio & Tokio causing wasm build to fail
- Consider only changing patch versions when making non breaking releases
- Feature Request: bool function argument that forces a cache refresh. HOT 2
- Update cached value asynchronously, outside the thread that returns the return value of the function HOT 1
- How to get all cached values? HOT 1
- Feature Request: Floating-Point ttl HOT 4
- Add feature for usage of `redis::aio::ConnectionManager` to help to recover from redis server restarts HOT 1
- `async_once` is unsound HOT 2
- Allow reexport
- sync_writes isn't working correctly when different values for function parameters are used HOT 5
- Tokio included when not using async HOT 1
- Documentation on docs.rs for `stores` module does not exist
- Cannot use `inner` as parameter name HOT 2
- Any plan on Set collection? HOT 1
- Retrieve cache expiration time from cached function result
- Also generate the fresh data function HOT 2
- Cannot call *_no_cache function HOT 3
- `failed to resolve: could not find `async_sync` in `cached`` HOT 2
- Once with sync_writes and async causes deadlocks in tokio.
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 cached.