An open API service indexing awesome lists of open source software.

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

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

cache function-cache ie8-compat memory storage

Last synced: about 1 month ago
JSON representation

function cache

Awesome Lists containing this project

README

          

NQ-CACHE

> function cache

[![build status](https://api.travis-ci.org/nqdy666/nq-cache.svg?branch=master)](https://travis-ci.org/nqdy666/nq-cache)
[![codecov](https://codecov.io/gh/nqdy666/nq-cache/branch/master/graph/badge.svg)](https://codecov.io/gh/nqdy666/nq-cache)
[![version](https://img.shields.io/npm/v/nq-cache.svg)](https://www.npmjs.com/package/nq-cache)
[![download](https://img.shields.io/npm/dm/nq-cache.svg)](https://www.npmjs.com/package/nq-cache)
[![David deps](https://img.shields.io/david/nqdy666/nq-cache.svg?style=flat-square)](https://david-dm.org/nqdy666/nq-cache)
[![license](https://img.shields.io/npm/l/nq-cache.svg)](https://www.npmjs.com/package/nq-cache)

> [CN 中文版](./README.zh_CN.md)

## Features
- IE8+
- Support for Typescript

## Document
- [Example on JSBin] (https://jsbin.com/baluray/edit?html,js,output)

## Installation

Install npm package

```bash
npm install nq-cache
```

Use `pureFuncMemoryCache`

add.js
```javascript
import { pureFuncMemoryCache } from 'nq-cache'

export function add (a, b) {
  return a + b
}

export const addCache = pureFuncMemoryCache(add)
```

app.js
```javascript
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
```javascript
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
```javascript
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
```javascript
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
```javascript
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`

```html

  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

```
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
```html

```

### Methods
- pureFuncMemoryCache
- promiseMemoryCache
- promiseSessionStorageCache
- clearCache
- argToKey

## Local development

- Installation dependencies

```bash
npm install
```

- Testing

```bash
npm test
```

- Build

```bash
npm run build
```

- `Flow`

```bash
npm run flow
```

- `ESLint`

```bash
npm run lint
```

- Update documentation

```bash
npm run doc
```

- Run the test page

```bash
npm run build
npm run example

Then open it with a browser
Http://localhost:5000/examples/
```

- Release

```bash
npm version [new version]
npm run build
npm publish
```

## Donation
If you find it useful, you can buy us a cup of coffee.

donation