NQ-CACHE 函数缓存,支持纯函数和返回值为Promise的函数

NQ-CACHE

函数缓存,支持纯函数和返回值为Promise的函数

项目地址

https://github.com/nqdy666/nq-cache

特性

  • IE8+
  • 支持Typescript

文档

  • Example on JSBin

安装

安装npm包

npm install nq-cache

使用 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) // 直接从缓存中获取结果

使用 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' }) 
})

使用 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

仅包含 nq-cache






其他更多的方法,可以查看例子

提示,如果浏览器不支持 Promise 或者 JSON,你应该进行 polyfill



方法

  • pureFuncMemoryCache
  • promiseMemoryCache
  • promiseSessionStorageCache
  • clearCache
  • argToKey

本地开发

  • 安装依赖
npm install
  • 测试
npm test
  • 打包
npm run build
  • Flow
npm run flow
  • ESLint
npm run lint
  • 更新文档
npm run doc
  • 运行测试页面
npm run build
npm run example

然后用浏览器打开
http://localhost:5000/examples/
  • 发布
npm version [new version]
npm run build
npm publish

你可能感兴趣的:(NQ-CACHE 函数缓存,支持纯函数和返回值为Promise的函数)