Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Removed snappy and protobuf from the main library and moved them
Browse files Browse the repository at this point in the history
into independent modules.
  • Loading branch information
suprememoocow committed Nov 18, 2015
1 parent 8614f69 commit 148fee8
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 105 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ looked up in the cache.
* Otherwise the response is cached and the Vary headers for the URL endpoint are stored
the response is returned to the caller.

# Support for snappy compression and/or protobuf serialization

Snappy compression and protobuf serialization of cached data is supported. However
in tests, it is generally slower than JSON and no compression, so it's disabled
by default.

To enable it, use one of both of the `serializer` and/or `compressor` options:
```js
var httpRequestCache = new RequestHttpCache({
backend: 'redis',
serializer: require('request-http-cache-protobuf-serializer'),
compressor: require('request-http-cache-snappy-compressor')
});
```

You will need to require the respective modules as these are no longer shipped
with `request-http-cache`.

# TODO

* Support for `If-Modified-Since` conditional responses (currently only uses ETags)
Expand Down
11 changes: 0 additions & 11 deletions lib/http-cache-message.desc

This file was deleted.

13 changes: 0 additions & 13 deletions lib/http-cache-message.proto

This file was deleted.

41 changes: 0 additions & 41 deletions lib/protobuf-serializer.js

This file was deleted.

24 changes: 4 additions & 20 deletions lib/redis-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var RESPONSE_TIME_KEY = "rt";
var URL_KEY = "u";
var DEFAULT_TTL = 86400;

var JSONBSerializer = require('./jsonb-serializer');
var NullCompressor = require('./null-compressor');

function bufferToString(buffer) {
if (!buffer) return null;
Expand All @@ -34,26 +36,8 @@ function RedisBackend(options) {

this.ttl = options.ttl || DEFAULT_TTL;

/* Choose a serializer */
var Serializer;

if (options.serializer === 'protobuf') {
Serializer = require('./protobuf-serializer');
} else {
Serializer = require('./jsonb-serializer');
}
this.serializer = new Serializer();

/* Choose a compressor */
var Compressor;

if (options.serializer === 'snappy') {
Compressor = require('./snappy-compressor');
} else {
Compressor = require('./null-compressor');
}
this.compressor = new Compressor();

this.serializer = options.serializer && new options.serializer() || new JSONBSerializer();
this.compressor = options.compressor && new options.compressor() || new NullCompressor();
}

RedisBackend.prototype = {
Expand Down
18 changes: 0 additions & 18 deletions lib/snappy-compressor.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"mocha": "^2.1.0",
"nock": "^0.59.1",
"request": "^2.53.0",
"request-http-cache-protobuf-serializer": "^0.1.1",
"request-http-cache-snappy-compressor": "^0.1.1",
"sinon": "^1.13.0",
"speedy": "^0.1.1"
},
Expand All @@ -38,9 +40,7 @@
"json-buffer": "^2.0.11",
"lodash": "^3.3.1",
"lru-cache": "^2.5.0",
"protobuf": "^0.11.0",
"redis": "^0.12.1",
"snappy": "^3.0.8",
"wreck": "^5.2.0"
}
}
31 changes: 31 additions & 0 deletions test/perf/redis-jsonb-snappy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var speedy = require('speedy');

var RequestHttpCache = require('../..');
var request = require('request');

var httpRequestCache = new RequestHttpCache({
backend: 'redis',
compressor: require('request-http-cache-snappy-compressor'),
redisPrefix: 'hc:' + Date.now() + ":"
});


function makeRequest(token, callback) {
httpRequestCache.extension({
method: 'GET',
url: 'https://api.github.com/user/repos?per_page=100',
headers: {
Authorization: 'token ' + token,
'User-Agent': 'request-http-cache-test/1.0',
},
json: true
}, callback, request);
}

makeRequest(process.env.GITHUB_TOKEN_1, function() {
setTimeout(function() {
speedy.run(function(done) {
makeRequest(process.env.GITHUB_TOKEN_1, done);
});
}, 500);
});
File renamed without changes.
32 changes: 32 additions & 0 deletions test/perf/redis-protobuf-snappy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var speedy = require('speedy');

var RequestHttpCache = require('../..');
var request = require('request');

var httpRequestCache = new RequestHttpCache({
backend: 'redis',
serializer: require('request-http-cache-protobuf-serializer'),
compressor: require('request-http-cache-snappy-compressor'),
redisPrefix: 'hc:' + Date.now() + ":"
});


function makeRequest(token, callback) {
httpRequestCache.extension({
method: 'GET',
url: 'https://api.github.com/user/repos?per_page=100',
headers: {
Authorization: 'token ' + token,
'User-Agent': 'request-http-cache-test/1.0',
},
json: true
}, callback, request);
}

makeRequest(process.env.GITHUB_TOKEN_1, function() {
setTimeout(function() {
speedy.run(function(done) {
makeRequest(process.env.GITHUB_TOKEN_1, done);
});
}, 500);
});
31 changes: 31 additions & 0 deletions test/perf/redis-protobuf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var speedy = require('speedy');

var RequestHttpCache = require('../..');
var request = require('request');

var httpRequestCache = new RequestHttpCache({
backend: 'redis',
serializer: require('request-http-cache-protobuf-serializer'),
redisPrefix: 'hc:' + Date.now() + ":"
});


function makeRequest(token, callback) {
httpRequestCache.extension({
method: 'GET',
url: 'https://api.github.com/user/repos?per_page=100',
headers: {
Authorization: 'token ' + token,
'User-Agent': 'request-http-cache-test/1.0',
},
json: true
}, callback, request);
}

makeRequest(process.env.GITHUB_TOKEN_1, function() {
setTimeout(function() {
speedy.run(function(done) {
makeRequest(process.env.GITHUB_TOKEN_1, done);
});
}, 500);
});

0 comments on commit 148fee8

Please sign in to comment.