nq-cache
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

NQ-CACHE

function cache

build status codecov node version David deps license

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

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

Package Sidebar

Install

npm i nq-cache

Weekly Downloads

2

Version

0.0.3

License

MIT

Unpacked Size

59.3 kB

Total Files

31

Last publish

Collaborators

  • nianqin