Coder Social home page Coder Social logo

nq-cache's Introduction

NQ-CACHE

function cache

build status codecov version download David deps license

CN 中文版

Features

  • IE8+
  • Support for Typescript

Document

Installation

Install npm package

npm install nq-cache

Use pureFuncMemoryCache

add.js

import { pureFuncMemoryCache } from 'nq-cache'

export function add (a, b) {
  return a + b
}

export const addCache = pureFuncMemoryCache(add)

app.js

import { addCache as add } from './add'
add(1, 2) // execute and cache the result
add(1, 2) // Get results directly from the cache

use promiseMemoryCache

request.js

import { promiseMemoryCache } from 'nq-cache'

export function request (data) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data)
    }, 2 * 1000)
  })
}

export const requestCache = promiseMemoryCache(request)

app.js

import { requestCache as request } from './request'
// execute and cache the result
request({ name: 'bowl' }).then(res => {
  // get results directly from the cache
  return request({ name: 'bowl' })
})

use promiseSessionStorageCache

request.js

import { promiseSessionStorageCache } from 'nq-cache'

export function request (data) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(data)
    }, 2 * 1000)
  })
}

export const requestCache = promiseSessionStorageCache(request, 'request')

app.js

import { requestCache as request } from './request'
// execute and cache the result
request({ name: 'bowl' }).then(res => {
  // Get results directly from the cache
  return request({ name: 'bowl' })
})

CDN

Contains only nq-cache

<!-- Use the latest version -->
<script src="https://unpkg.com/nq-cache@latest"></script>
<!-- or specify a version -->
<script src="https://unpkg.com/[email protected]"></script>
<script>
  function add (a, b) {
    return a + b
  }
  addCache = cache.pureFuncMemoryCache(add)
  addCache(1, 2) // Execute and cache the result
  addCache(1, 2) // Get the result directly from the cache
</script>

For more other methods, you can view [example] (https://jsbin.com/baluray/edit?html,js,output)

if the browser does not support Promise or JSON, you should do a polyfill

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<!--[if lt IE 8]>
  <script type="text/javascript" src="https://cdn.bootcss.com/json2/20160511/json2.min.js"></script>
<![endif]-->

Methods

  • pureFuncMemoryCache
  • promiseMemoryCache
  • promiseSessionStorageCache
  • clearCache
  • argToKey

Local development

  • Installation dependencies
npm install
  • Testing
npm test
  • Build
npm run build
  • Flow
npm run flow
  • ESLint
npm run lint
  • Update documentation
npm run doc
  • Run the test page
npm run build
npm run example

Then open it with a browser
Http://localhost:5000/examples/
  • Release
npm version [new version]
npm run build
npm publish

Donation

If you find it useful, you can buy us a cup of coffee.

donation

nq-cache's People

Contributors

nqdy666 avatar

Stargazers

 avatar  avatar

Watchers

 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.