Code Monkey home page Code Monkey logo

cache-hit's Introduction

cache-hit

JavaScript promise caching library

Types

options

Option Required Description Default value
timeToLive No An integer value that the cache is valid for in milliseconds Number.POSITIVE_INFINITY

Omitting timeToLive Option

This will result in an infinite cache. Once a successful response is received, the promise will never be invoked a second time.

Setting timeToLive to 0

This will result in the result of the promise not being cached. The promise will always be invoked again a second time.

Cache

interface Cache {
  read: () => Promise
}

API

createCache(promiseReturningFunction: () => Promise, options): Cache

PARAMETERS

promiseReturningFunction

A function that when called, will return a promise. The promiseReturningFunction is invoked from within the library.

options

Object of options. Supported options are listed in the Types section.

Return Value

Returns a Cache type.

Cache.read({ key: string, forceInvoke: Boolean }, ...promiseParameters: any): Promise

PARAMETERS

key

Data returned will be stored in the cache based on a key. Whenever reads occur, the cache will first be checked for a valid cache value and return the cached value or in the case of an invalid cache, the promise will be invoked and data will be stored in the cache depending on your timeToLive option.

forceInvoke

Defaulted to false. If this is set to true, the promise will be invoked regardless of the caching policy.

promiseParameters

Parameters that will be spread into the promiseReturningFunction. An example of calling this function exists below.

getAccountsApiCached.read({ key: 'some-key' }, param1, param2);

Example

// get-accounts.js
import createCache from 'cache-hit';

const getAccountsApi = (username, sessionId) => fetch(`/accounts/${username}`, { headers: { sessionId });

const getAccountsApiCached = createCache(getAccountsApi, { timeToLive: 15000 }); // timeToLive in milliseconds

export default getAccountsApiCached;
// accounts.js
import getAccountsApiCached from './get-accounts';

const getAccounts = (username, sessionId) => {
  const key = username; // unique key for this cache instance
  return getAccountsApi
          .read({ key: key, forceInvoke: false }, username, sessionId)
          .then((response) => dispatch(someAction(response)))
          .catch((error) => dispatch(someErrorAction(error)));
};

Errors

All errors will need to be handled after calling the read method. The cache will not be updated on any rejected promises.

cache-hit's People

Contributors

strange-developer avatar hvolschenk avatar lylegk 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.