Skip to content

t2ee/client-cache

Repository files navigation

Install

npm i bulkcache -S

Description

Allows painlessly cache request reuslt or any result that have a unique id. Saves client/service resources by minizing network request, and requested content.

Usage

import { Cache, FilterFunction } from 'bulkcache';

interface User {
    id: number;
    name: string;
    email: string;
}

function idGetter(user: User) {
    return user.id;
}

async function fetch(id: number) {
    return await YourAPI.fetchUser(id);
}

async function bulkFetch(ids: number[]) {
    return await YourAPI.fetchALotOfUsers(ids);
}

function allIWantIsANameFilter(user: User) {
    return user.name;
}

const cache = new Cache(idGetter, fetch, bulkFetch);
cache.addFilter(FilterFunction(allIWantIsANameFilter));

 // open your Chrome Developer tools, witness miracles!
console.log(await cache.fetch(1)); // this should produce one network request
console.log(await cache.fetch(1)); // this should not produce any network request

cache.fetch(2);// this should produce one network request. NOTICE!!!, we did not wait for its network request to return
console.log(await cache.fetch(2)); // this should not produce any network request

console.log(await cache.batchGet([1, 2])) // this should not produce any network request
console.log(await cache.batchGet([1, 2, 3, 4])) // this should produce one network request with batch requerst for id 3 and 4.

Contribute

Issues and PRs are welcome! If you feel any functionalities that are crucial, you are also welcome to submit an Issue about it.

About

cache api requested result painlessly.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published