Git Product home page Git Product logo

Comments (7)

jaemk avatar jaemk commented on July 30, 2024

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);
    }
}

from cached.

linclelinkpart5 avatar linclelinkpart5 commented on July 30, 2024

@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.

from cached.

jaemk avatar jaemk commented on July 30, 2024

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

from cached.

jaemk avatar jaemk commented on July 30, 2024

added in #14

from cached.

emirror-de avatar emirror-de commented on July 30, 2024

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

from cached.

jaemk avatar jaemk commented on July 30, 2024

@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()
}

from cached.

emirror-de avatar emirror-de commented on July 30, 2024

Awesome! Thank you!

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.