https://github.com/bencode/yield-cache
Cache utility for generator
https://github.com/bencode/yield-cache
Last synced: 12 months ago
JSON representation
Cache utility for generator
- Host: GitHub
- URL: https://github.com/bencode/yield-cache
- Owner: bencode
- Created: 2015-06-16T13:56:37.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-04-09T05:47:08.000Z (almost 10 years ago)
- Last Synced: 2024-04-19T16:18:15.192Z (almost 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# yield-cache
[](https://travis-ci.org/bencode/yield-cache)
[](https://coveralls.io/r/bencode/yield-cache)
Cache utility for generator
plover \ node | 5.x | 4.x | 0.12.x
--- | --- | --- | ----
0.x.x | √ | √ | √
1.x.x | √ | √ | X
```shell
$ npm install yield-cache
```
## API
### yieldCache()
create cache instance
```
const cache = yieldCache();
```
### cache(key, obj)
try get item from cache or yeild from obj
### cache.remove(key)
remove cache item for key
## Useage
1\. Create an instance for cache a group of generator
```js
const cache = yieldCache();
```
2\. Use cache instance
```js
// it should used in generator function
function* () {
// call with cackeKey and yieldable object
const item = yield* cache(cacheKey, Generator or GeneratorFunction or Functin that return Promise);
}
```
## Example
```js
const yieldCache = require('yield-cache');
// create an instance
const renderCache = yieldCache();
// use
function* getRender(path) {
const render = yield* renderCache(path, function* () {
const tpl = yield fs.readFile(path, 'utf-8');
return compiler.complie(tpl);
});
return render;
}
const path = ...
const render = yield* getRender(path);
const render2 = yield* getRender(path);
render.should.equal(render2);
```