Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit cce7c7ce802777522f367475d04211c2af5b6e21 1 parent a454136
Arnout Kazemier authored October 17, 2012
6  README.md
Source Rendered
@@ -46,4 +46,10 @@ cache.start();
46 46
 
47 47
 // kill everything, nuke that motherfucker.
48 48
 cache.destroy();
  49
+
  50
+// OH so you want to store the output of a Stream? sure!
  51
+var stream = cache.stream('key', fs.createReadStream(..), '10 seconds');
  52
+
  53
+// stream is the result of fs.createReadStream
  54
+// once the stream fires it's `done` event, we will store the data.
49 55
 ```
36  index.js
@@ -33,6 +33,9 @@ Expire.prototype.get = function get(key, dontUpdate) {
33 33
 
34 34
   var now = Date.now();
35 35
 
  36
+  // We are still streaming in data, so return nothing
  37
+  if (result.streaming) return undefined;
  38
+
36 39
   // We found a match, make sure that it's not expired.
37 40
   if (now - result.last >= result.expires) {
38 41
     delete this.cache[key];
@@ -66,6 +69,38 @@ Expire.prototype.set = function set(key, value, expires) {
66 69
 };
67 70
 
68 71
 /**
  72
+ * Stores the complete output of a stream in memory.
  73
+ *
  74
+ * @param {String} key the key
  75
+ * @param {Stream} stream the stream that we need to read the data off
  76
+ * @param {String} expires option custom expire
  77
+ * @returns {Stream} the stream you passed it
  78
+ */
  79
+Expire.prototype.stream = function streamer(key, stream, expires) {
  80
+  var chunks = []
  81
+    , self = this;
  82
+
  83
+  this.cache[key] = { streaming: true };
  84
+
  85
+  stream.on('data', function data(buffer) {
  86
+    chunks.push(buffer);
  87
+  });
  88
+
  89
+  stream.on('error', function error() {
  90
+    chunks.length = 0;
  91
+  });
  92
+
  93
+  stream.on('end', function end(buffer) {
  94
+    if (buffer) chunks.push(buffer);
  95
+
  96
+    if (chunks.length) self.set(key, Buffer.concat(chunks), expires);
  97
+    chunks.length = 0;
  98
+  });
  99
+
  100
+  return stream;
  101
+};
  102
+
  103
+/**
69 104
  * Checks if the item exists in the cache.
70 105
  *
71 106
  * @param {String} key
@@ -117,6 +152,7 @@ Expire.prototype.scan = function scan() {
117 152
   for (key in this.cache) {
118 153
     result = this.cache[key];
119 154
 
  155
+    if (result.streaming) continue;
120 156
     if (now - result.last >= result.expires) {
121 157
       delete this.cache[key];
122 158
     }
2  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "expirable",
3  
-  "version": "0.0.2",
  3
+  "version": "0.0.3",
4 4
   "description": "Expirable cache",
5 5
   "main": "index.js",
6 6
   "scripts": {
20  tests/expirable.test.js
@@ -90,4 +90,24 @@ describe('Expirable', function () {
90 90
       done();
91 91
     }, 50);
92 92
   });
  93
+
  94
+  it('should store the data from a stream as a buffer', function (done) {
  95
+    var cache = new Expirable('10 minutes')
  96
+      , fs = require('fs');
  97
+
  98
+    var stream = cache.stream('foo', fs.createReadStream(__filename));
  99
+
  100
+    expect(stream).to.instanceof(require('stream'));
  101
+    expect(cache.get('foo')).to.equal(undefined);
  102
+
  103
+    stream.on('end', function () {
  104
+      var buff = cache.get('foo');
  105
+
  106
+      expect(Buffer.isBuffer(buff)).to.equal(true);
  107
+      expect(buff.toString()).to.contain('fs.createReadStream(__filename));');
  108
+
  109
+      cache.destroy();
  110
+      done();
  111
+    });
  112
+  });
93 113
 });

0 notes on commit cce7c7c

Please sign in to comment.
Something went wrong with that request. Please try again.