Permalink
Browse files

[api] Added support for caching the output of streams. Releases 0.0.3

  • Loading branch information...
1 parent a454136 commit cce7c7ce802777522f367475d04211c2af5b6e21 @3rd-Eden committed Oct 17, 2012
Showing with 63 additions and 1 deletion.
  1. +6 −0 README.md
  2. +36 −0 index.js
  3. +1 −1 package.json
  4. +20 −0 tests/expirable.test.js
View
@@ -46,4 +46,10 @@ cache.start();
// kill everything, nuke that motherfucker.
cache.destroy();
+
+// OH so you want to store the output of a Stream? sure!
+var stream = cache.stream('key', fs.createReadStream(..), '10 seconds');
+
+// stream is the result of fs.createReadStream
+// once the stream fires it's `done` event, we will store the data.
```
View
@@ -33,6 +33,9 @@ Expire.prototype.get = function get(key, dontUpdate) {
var now = Date.now();
+ // We are still streaming in data, so return nothing
+ if (result.streaming) return undefined;
+
// We found a match, make sure that it's not expired.
if (now - result.last >= result.expires) {
delete this.cache[key];
@@ -66,6 +69,38 @@ Expire.prototype.set = function set(key, value, expires) {
};
/**
+ * Stores the complete output of a stream in memory.
+ *
+ * @param {String} key the key
+ * @param {Stream} stream the stream that we need to read the data off
+ * @param {String} expires option custom expire
+ * @returns {Stream} the stream you passed it
+ */
+Expire.prototype.stream = function streamer(key, stream, expires) {
+ var chunks = []
+ , self = this;
+
+ this.cache[key] = { streaming: true };
+
+ stream.on('data', function data(buffer) {
+ chunks.push(buffer);
+ });
+
+ stream.on('error', function error() {
+ chunks.length = 0;
+ });
+
+ stream.on('end', function end(buffer) {
+ if (buffer) chunks.push(buffer);
+
+ if (chunks.length) self.set(key, Buffer.concat(chunks), expires);
+ chunks.length = 0;
+ });
+
+ return stream;
+};
+
+/**
* Checks if the item exists in the cache.
*
* @param {String} key
@@ -117,6 +152,7 @@ Expire.prototype.scan = function scan() {
for (key in this.cache) {
result = this.cache[key];
+ if (result.streaming) continue;
if (now - result.last >= result.expires) {
delete this.cache[key];
}
View
@@ -1,6 +1,6 @@
{
"name": "expirable",
- "version": "0.0.2",
+ "version": "0.0.3",
"description": "Expirable cache",
"main": "index.js",
"scripts": {
View
@@ -90,4 +90,24 @@ describe('Expirable', function () {
done();
}, 50);
});
+
+ it('should store the data from a stream as a buffer', function (done) {
+ var cache = new Expirable('10 minutes')
+ , fs = require('fs');
+
+ var stream = cache.stream('foo', fs.createReadStream(__filename));
+
+ expect(stream).to.instanceof(require('stream'));
+ expect(cache.get('foo')).to.equal(undefined);
+
+ stream.on('end', function () {
+ var buff = cache.get('foo');
+
+ expect(Buffer.isBuffer(buff)).to.equal(true);
+ expect(buff.toString()).to.contain('fs.createReadStream(__filename));');
+
+ cache.destroy();
+ done();
+ });
+ });
});

0 comments on commit cce7c7c

Please sign in to comment.