Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to clear/defeat cache stores #10

Closed
linclelinkpart5 opened this issue Sep 25, 2018 · 7 comments
Closed

Ability to clear/defeat cache stores #10

linclelinkpart5 opened this issue Sep 25, 2018 · 7 comments

Comments

@linclelinkpart5
Copy link
Contributor

I was wondering if there are plans to add functionality to clear caches programatically, or be able to force recomputation/defeat the cache on certain calls?

I'd love to contribute as I can, but I wanted to make sure this fit within the scope of what this crate intends to do. :)

@jaemk
Copy link
Owner

jaemk commented Sep 26, 2018

That's certainly something that could be added in some capacity! Maybe adding a Cached::cache_reset trait method? I'd be a little hesitant to add that logic into the macro though since you could manually grab a lock and mutate the cache from within the cached! function:

cached! {
    WOMP: SizedCache<(u32), u32> = SizedCache::with_size(50);
    fn womp(n: u32) -> u32 = {
        use cached::Cached;
        let mut cache = WOMP.lock().unwrap();
        // this could be resetting instead
        cache.cache_set((1), 1);
        cache.cache_set((2), 2);
        cache.cache_set((3), 3);
        n
    }
}

pub fn main() {
    womp(10);
    {
        use cached::Cached;
        let cache = WOMP.lock().unwrap();
        println!("size=4 -> {:?}", cache.cache_size() == 4);
    }
}

@linclelinkpart5
Copy link
Contributor Author

@jaemk I see, is there a way to add that trait method without adding it to the macro invocation? Not sure I understand, I'm unfamiliar with creating custom Rust macros.

@jaemk
Copy link
Owner

jaemk commented Sep 28, 2018

Yes, the trait method could be added here in lib.rs and then implemented for all the stores in stores.rs. The macros wouldn't need to be updated

@jaemk
Copy link
Owner

jaemk commented Oct 5, 2018

added in #14

@jaemk jaemk closed this as completed Oct 5, 2018
@emirror-de
Copy link

Is there a way to use this functionality in combination with the proc-macro?

@jaemk
Copy link
Owner

jaemk commented Nov 6, 2023

@emirror-de the cache_clear and cache_reset methods are available on cache types. Which macro was used doesn't matter. The proc macros will generate a global identifier either equal to the name argument passed to the macro or equal to the function name uppercased.

use cached::Cached;
use cached::proc_macro::cached;

#[cached]
fn my_func() -> i32 { ... }

#[cached(name = "MY_FUNC_2")]
fn my_funky_func() -> i32 { ... }

{
    MY_FUNC.lock().unwrap().cache_reset()
    MY_FUNC_2.lock().unwrap().cache_reset()
}

@emirror-de
Copy link

Awesome! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants