Skip to content

Add a way to clear the cached values? #13

@SimonGustavsson

Description

@SimonGustavsson

Hey!
Cool package, thanks for that!

I have a suggestion...

In a long running node process it might be useful to memoize things like DB/file access. I appreciate this might not be a popular/expected thing to do at all, but I like the neatness of caching these things with a decorator! (Currently just playing around with this package).

So, these things can obviously change so it'd be neat to be able to clear this cache. Maybe after an interval, though that doesn't have to be a part of the package as long as there's a way of clearing it (wouldn't mind specifying @Memoize(timeToLive:480) in the decorator though!)

I quickly whipped this up that'll blast all cached maps that does the trick for me right now. It might be nice to just clear the cache for a specific function, but because the maps don't store the function name that's a bit trickier right now! :-)

export function clear<T extends { [key: string]: any }>(obj: T) {
    const keys = Object.getOwnPropertyNames(obj);
    const stub = '__memoized_map_';

    for (const key of keys) {
        if (key !== undefined && key.startsWith(stub)) {
            delete obj[key];
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions