Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nq-cache

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nq-cache

function cache

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

NQ-CACHE

函数缓存

build status codecov node version David deps license

Build Status

特性

  • IE8+
  • 支持Typescript
  • 支持纯函数缓存
  • 支持返回值Promise的函数缓存
  • 支持缓存到sessionStorage

文档

安装

安装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

<!-- 使用最新版本 -->
<script src="https://unpkg.com/nq-cache@latest"></script>
<!-- 或指定某一个版本 -->
<script src="https://unpkg.com/nq-cache@0.0.2"></script>
<script>
  function add (a, b) {
    return a + b
  }
  addCache = cache.pureFuncMemoryCache(add)
  addCache(1, 2) // 执行,并把结果缓存
  addCache(1, 2) // 直接从缓存中获取结果
</script>

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

提示,如果浏览器不支持 Promise 或者 JSON,你应该进行 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]-->

本地开发

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

Keywords

FAQs

Package last updated on 21 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc