https://github.com/heapwolf/through-cache
a through stream that caches
https://github.com/heapwolf/through-cache
Last synced: 8 months ago
JSON representation
a through stream that caches
- Host: GitHub
- URL: https://github.com/heapwolf/through-cache
- Owner: heapwolf
- Created: 2013-11-24T06:13:32.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2013-11-24T18:52:07.000Z (about 12 years ago)
- Last Synced: 2025-03-29T19:34:27.626Z (9 months ago)
- Language: JavaScript
- Size: 137 KB
- Stars: 20
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SYNOPSIS
A through stream that caches
# MOTIVATION
There are some cases where the same data is piped over and over again.
In these cases there is no reason to repeat the transformation of the
data when the result can be retrieved from a cache.
# USAGE
Create a cache and pipe to its `cache` method.
```js
//
// create a new cache
//
var tc = require('through-cache')()
a.pipe(tc.cache(function(obj) {
//
// this only gets called once because
// the exact same data is written again.
//
// but two writes are made to the `b` stream,
// one from the `a` stream and one from the
// cache.
//
var str = '
' + obj.value + ''
this.push(str)
}).pipe(b)
stream.write({ value: 'hello, world' })
stream.write({ value: 'hello, world' })
```
To cache a function that will get applied to the data.
```js
a.pipe(tc.cache(function(obj) {
// the transform function will get cached and then
// applied to the data every time data gets pushed
// to the stream.
this.transform(function(data) {
return '
' + data + ''
})
this.push(obj.value)
}).pipe(b)
```
And to invalidate the cache.
```
tc.invalidate()
```