1.3.5 • Published 5 years ago

bulkcache v1.3.5

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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.

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

0.1.0

5 years ago