Skip to content

nqdy666/nq-cache

Repository files navigation

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/nq-cache@0.0.3"></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