Git Product home page Git Product logo

Comments (1)

jaemk avatar jaemk commented on July 30, 2024

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

cached/tests/cached.rs

Lines 1094 to 1128 in 8295ad2

#[cached(size = 2, time = 1, key = "String", convert = r#"{ String::from(s) }"#)]
fn cached_timed_sized_prime(s: &str) -> bool {
s == "true"
}
#[test]
fn test_cached_timed_sized_prime() {
assert!(cached_timed_sized_prime("true"));
{
let cache = CACHED_TIMED_SIZED_PRIME.lock().unwrap();
assert_eq!(cache.cache_hits(), Some(0));
assert_eq!(cache.cache_misses(), Some(1));
}
assert!(cached_timed_sized_prime("true"));
{
let cache = CACHED_TIMED_SIZED_PRIME.lock().unwrap();
assert_eq!(cache.cache_hits(), Some(1));
assert_eq!(cache.cache_misses(), Some(1));
}
std::thread::sleep(std::time::Duration::from_millis(500));
assert!(cached_timed_sized_prime_prime_cache("true"));
std::thread::sleep(std::time::Duration::from_millis(500));
assert!(cached_timed_sized_prime_prime_cache("true"));
std::thread::sleep(std::time::Duration::from_millis(500));
assert!(cached_timed_sized_prime_prime_cache("true"));
// stats unchanged (other than this new hit) since we kept priming
assert!(cached_timed_sized_prime("true"));
{
let cache = CACHED_TIMED_SIZED_PRIME.lock().unwrap();
assert_eq!(cache.cache_hits(), Some(2));
assert_eq!(cache.cache_misses(), Some(1));
}
}

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

cached/src/lib.rs

Lines 283 to 294 in 8295ad2

#[cached(time = 60, 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));
keyed_prime_cache("a");
}
});
}

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)

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.