Comments (1)
That's an interesting case. I've just released some changes to support this in 0.27.0
. Unfortunately, it's not something simple enough that I think it should handled completely by the #[cached]
macro since refreshing the value would involve blocking the current thread, or knowing how not to block the current thread (new thread / async task).
Instead of adding that complexity to the macro itself, the macro now generates an additional function *_prime_cache
to let you forcefully update the cache yourself. See
Lines 1094 to 1128 in 8295ad2
So to get the functionality you're looking for you can manually run a thread of logic with your preferred concurrency mechanism (threads or async) to continuously update the cache for a specific set of arguments or for a #[once]
singleton like this example from the readme
Lines 283 to 294 in 8295ad2
The one "downside" of this is that you must prime the cache using every distinct set of function arguments. If you'd like to always re-prime every key, you can do so by iterating over the cache keys directly and passing those keys as function arguments like so
use std::thread::sleep;
use std::time::Duration;
use cached::proc_macro::cached;
#[cached(key = "String", convert = r#"{ String::from(a) }"#)]
fn keyed(a: &str) -> usize {
a.len()
}
pub fn main() {
std::thread::spawn(|| {
loop {
sleep(Duration::from_secs(60));
let keys: Vec<String> = {
// note the cache keys are a tuple of all function arguments, unless it's one value
KEYED.lock().unwrap().get_store().keys().map(|k| k.clone()).collect()
};
for k in &keys {
keyed_prime_cache(k);
}
}
});
}
from cached.
Related Issues (20)
- 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.
- Unnecessary `&mut V` with `get_or_set_with` and `try_get_or_set_with` (CachedAsync)
- Feature: Ability to configure (or reconfigure) a SizedCache `size` based on runtime data HOT 4
- 2021 edition?
- Async disk cache HOT 4
- Add helper attribute to ignore arguments HOT 1
- Disk Cache does not persist between runs HOT 1
- Borrowed keys and values for `IOCached::set_cache` HOT 1
- Cache clear operation
- proc_macro: support args which are &T and Option<&T>
- [Question] Thread safety? HOT 3
- Redis cache doesn't support patterns HOT 1
- DiskCache blobs aren't cleaned on overwrite HOT 8
- Why does `DiskCache` need to impl `Display`? HOT 6
- [Discussion] What do you think of removing `async-std` support?
- Cache hit / miss rate metrics when using `cached` procedural macro HOT 3
- Generic parameters not added to no_cache function.
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.