NQ-CACHE
function cache
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)
add(1, 2)
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'
request({ name: 'bowl' }).then(res => {
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'
request({ name: 'bowl' }).then(res => {
return request({ name: 'bowl' })
})
CDN
Contains only nq-cache
<script src="https://unpkg.com/nq-cache@latest"></script>
<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)
addCache(1, 2)
</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>
Methods
- pureFuncMemoryCache
- promiseMemoryCache
- promiseSessionStorageCache
- clearCache
- argToKey
Local development
- Installation dependencies
npm install
npm test
npm run build
npm run flow
npm run lint
npm run doc
npm run build
npm run example
Then open it with a browser
Http://localhost:5000/examples/
npm version [new version]
npm run build
npm publish