How to use the @hpcc-js/util.Cache function in @hpcc-js/util

To help you get started, we’ve selected a few @hpcc-js/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hpcc-systems / Visualization / tests / test-util / src / cache.spec.ts View on Github external
it("basic", function () {
        class MyClass {
            id: string;
            id2: string;

            constructor(id: string, id2: string) {
                this.id = id;
                this.id2 = id2;
            }
        }
        const myCache = new Cache<{ id: string }, MyClass>((obj) => {
            return Cache.hash([obj.id]);
        });
        expect(myCache.has({ id: "007" })).is.false;
        const tmp = myCache.get({ id: "007" }, () => {
            return new MyClass("007", "a");
        });
        expect(myCache.has({ id: "007" })).is.true;
        expect(tmp.id2).equals("a");
        const tmp2 = myCache.get({ id: "007" }, () => {
            throw new Error("Should Not Happend");
        });
        expect(tmp2.id2).equals("a");
    });
});