Git Product home page Git Product logo

cachenado's Introduction

npm version Build Status Coverage Status

Cachenado

What it is

Cachenado is a small caching utility which allows you to connect a store / stage manager and function results. There is no data cache included in this library. Instead Cachenado has an included mapping cache which references your store key with functions hash.

What it is not

Cachenado will simply store a mapping between your store variable and a function name. The storing of this function return value is still something you should do, as you could transform this result before storing it.

Install

yarn add cachenado

How it works

Get a cache

First, you need a cache. Cachenado provides a utility to create it as follow :

// An object, the most basic store.
const myVariableStore = {
  applicationUserList: [],
};

// We map the "getUserList" function to the "applicationUserList" key in the store.
const funcNameToObservableNameMapping: { [key: string]: keyof typeof myVariableStore } = {
  getUserList: "applicationUserList",
};

// The cache know nows how to get the "getUserList" function return value from the store.
const cache = createCache(myVariableStore, funcNameToObservableNameMapping);

Or you can implement your own cache if you need to connect it to your store in a different way, it need to implements the following two functions :

get: (key: string) => { value: any; until: number } | undefined;
set: (key: string, obj: { value: any; until: number }) => any;

I plan to implement utility functions to createCache for Mobx or Redux stores but keep in mind that Cachenado is framework agnostic.

Add caching to your function

const getUserList = () => {
  return [{ name: "Michel" }, { name: "Michoul" }];
};

const memoizedGetUserList = timeCacheResult(cache, getUserList, {
  cacheTime: 5000, // In ms. Optionnal, default 10000
  nameToCache: "getMyUsers", // Optionnal, default is the cached function name
});

The second call to this function within 10 seconds and with the same arguments will instead use the get function provided by the cache to get the value stored. It will not call getUserList again.

License

MIT

cachenado's People

Contributors

sangrene avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.