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
- Host: GitHub
- URL: https://github.com/nqdy666/nq-cache
- Owner: nqdy666
- License: mit
- Created: 2018-05-17T08:01:40.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T04:29:06.000Z (over 3 years ago)
- Last Synced: 2025-10-03T15:46:07.513Z (6 months ago)
- Topics: cache, function-cache, ie8-compat, memory, storage
- Language: JavaScript
- Homepage: https://nqdy666.github.io/nq-cache/
- Size: 2.2 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
NQ-CACHE
> function cache
[](https://travis-ci.org/nqdy666/nq-cache)
[](https://codecov.io/gh/nqdy666/nq-cache)
[](https://www.npmjs.com/package/nq-cache)
[](https://www.npmjs.com/package/nq-cache)
[](https://david-dm.org/nqdy666/nq-cache)
[](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.