Skip to content

Commit

Permalink
feat: content-type are also cached (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutak23 authored Jan 11, 2024
1 parent 73388c4 commit 2e60c7b
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 2 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports.cacheSeconds = function (secondsTTL, cacheKey) {
if (value) {
// returns the value immediately
debug('hit!!', key)
if (value.contentType) res.set('Content-Type', value.contentType)
if (value.isJson) {
res.status(value.status).json(value.body)
} else {
Expand Down Expand Up @@ -102,7 +103,7 @@ module.exports.cacheSeconds = function (secondsTTL, cacheKey) {

didHandle = true
const body = data instanceof Buffer ? data.toString() : data
if (res.statusCode < 400) cacheStore.set(key, { status: res.statusCode, body: body, isJson: isJson }, ttl)
if (res.statusCode < 400) cacheStore.set(key, { status: res.statusCode, body: body, isJson: isJson, contentType: res.getHeader('Content-Type') }, ttl)

// send this response to everyone in the queue
drainQueue(key)
Expand Down Expand Up @@ -180,6 +181,7 @@ module.exports.cacheSeconds = function (secondsTTL, cacheKey) {
return cacheStore.get(key).then((cachedValue) => {
const value = cachedValue || {}
debug('>> queued hit:', key, value.length)
if (value.contentType) res.set('Content-Type', value.contentType)
if (value.isJson) {
res.status(value.status || 200).json(value.body)
} else {
Expand Down
29 changes: 29 additions & 0 deletions test/app-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var request = require('supertest'),

var hitIndex = 0;
var noContentIndex = 0;
var contentTypeIndex = 0;

var app = express();
var agent = request.agent(app);
Expand Down Expand Up @@ -43,6 +44,12 @@ describe('App-level cache:', function () {
res.status(204).json();
});

app.get('/content-type', function (req, res) {
contentTypeIndex++;
res.set('Content-Type', 'application/xml');
res.send('<xml><node>This is some content</node></xml>')
});

it('1st res.send', function (done) {
agent
.get('/app-test')
Expand Down Expand Up @@ -138,4 +145,26 @@ describe('App-level cache:', function () {
done()
})
})

it('1st content-type', function (done) {
agent
.get('/content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})

it('2st content-type', function (done) {
agent
.get('/content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})
});
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let testindex = 0
let noContentIndex = 0
let paramTestindex = 0
let testindexRemove = 0
let contentTypeIndex = 0

describe('# RouteCache middleware test', function () {
const app = express()
Expand All @@ -31,6 +32,12 @@ describe('# RouteCache middleware test', function () {
res.status(204).send()
})

app.get('/content-type', routeCache.cacheSeconds(10), function (req, res) {
contentTypeIndex++
res.set('Content-Type', 'application/xml')
res.send('<xml><node>This is some content</node></xml>')
})

app.get('/500', routeCache.cacheSeconds(10), function (req, res) {
res.status(500).send('Internal server error: ' + Math.random())
})
Expand Down Expand Up @@ -109,6 +116,28 @@ describe('# RouteCache middleware test', function () {
})
})

it('1st content-type', function (done) {
agent
.get('/content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})

it('2st content-type', function (done) {
agent
.get('/content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})

it('1st Redirect to hello', function (done) {
agent
.get('/redirect-to-hello')
Expand Down
40 changes: 40 additions & 0 deletions test/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var request = require('supertest'),

var paramTestIndex = 0
var noContentIndex = 0
var contentTypeIndex = 0

describe('Params / Querystrings', function () {
var app = express()
Expand All @@ -20,6 +21,12 @@ describe('Params / Querystrings', function () {
res.status(204).send()
})

app.get('/params-content-type', routeCache.cacheSeconds(10, '/params-test-content-type'), function (req, res) {
contentTypeIndex++
res.set('Content-Type', 'application/xml')
res.send('<xml><node>This is some content</node></xml>')
})

var agent = request.agent(app)

it('without params', function (done) {
Expand Down Expand Up @@ -69,4 +76,37 @@ describe('Params / Querystrings', function () {
done()
})
})

it('content-type without params', function (done) {
agent
.get('/params-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})

it('content-type with a=1', function (done) {
agent
.get('/params-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})

it('content-type with a=2', function (done) {
agent
.get('/params-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
})
})
118 changes: 117 additions & 1 deletion test/ttls.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('TTL:', function () {
context('disabled (-1)', function () {
var hitIndex = 0;
var noContentIndex = 0;
var contentTypeIndex = 0;

app.get('/cache-disabled', routeCache.cacheSeconds(-1), function (req, res) {
hitIndex++;
Expand All @@ -24,6 +25,12 @@ describe('TTL:', function () {
res.status(204).send();
});

app.get('/cache-disabled-content-type', routeCache.cacheSeconds(-1), function (req, res) {
contentTypeIndex++
res.set('Content-Type', 'application/xml')
res.send('<xml><node>This is some content</node></xml>')
})

it('Should get Hit #1', function (done) {
agent
.get('/cache-disabled')
Expand Down Expand Up @@ -62,11 +69,36 @@ describe('TTL:', function () {
});
});

it('Should contentTypeIndex is 1', function (done) {
agent
.get('/cache-disabled-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
});

it('Should contentTypeIndex is 2 (after nextTick)', function (done) {

process.nextTick(function () {
agent
.get('/cache-disabled-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 2)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
});
});
})

context('zero', function () {
var hitIndex = 0;
var noContentIndex = 0;
var contentTypeIndex = 0;

app.get('/cache-zero', routeCache.cacheSeconds(0), function (req, res) {
hitIndex++;
Expand All @@ -78,6 +110,12 @@ describe('TTL:', function () {
res.status(204).send();
});

app.get('/cache-zero-content-type', routeCache.cacheSeconds(0), function (req, res) {
contentTypeIndex++
res.set('Content-Type', 'application/xml')
res.send('<xml><node>This is some content</node></xml>')
})

it('Should get Hit #1', function (done) {
agent
.get('/cache-zero')
Expand Down Expand Up @@ -138,11 +176,48 @@ describe('TTL:', function () {
}, 200);
});

it('Should contentTypeIndex is 1', function (done) {
agent
.get('/cache-zero-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
});

it('Should contentTypeIndex is 1 (after nextTick)', function (done) {
process.nextTick(function () {
agent
.get('/cache-zero-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
});
});

it('Should contentTypeIndex is 2 (after 200ms delay)', function (done) {
setTimeout(function () {
agent
.get('/cache-zero-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 2)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
}, 200);
});
})

context('1 second:', function () {
var hitIndex = 0;
var noContentIndex = 0;
var contentTypeIndex = 0;

app.get('/cache-1s', routeCache.cacheSeconds(1), function (req, res) {
hitIndex++;
Expand All @@ -154,6 +229,12 @@ describe('TTL:', function () {
res.status(204).send();
});

app.get('/cache-1s-content-type', routeCache.cacheSeconds(1), function (req, res) {
contentTypeIndex++
res.set('Content-Type', 'application/xml')
res.send('<xml><node>This is some content</node></xml>')
})

it('Should get Hit #1', function (done) {
agent
.get('/cache-1s')
Expand Down Expand Up @@ -214,6 +295,41 @@ describe('TTL:', function () {
}, 1300);
});

});
it('Should contentTypeIndex is 1', function (done) {
agent
.get('/cache-1s-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
});

it('Should contentTypeIndex is 1 (after 200ms delay)', function (done) {
setTimeout(function () {
agent
.get('/cache-1s-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 1)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
}, 200);
});

it('Should contentTypeIndex is 2 (after 1200ms delay)', function (done) {
setTimeout(function () {
agent
.get('/cache-1s-content-type')
.expect(200, (error, response) => {
assert.equal(contentTypeIndex, 2)
assert.equal(response.headers['content-type'], 'application/xml; charset=utf-8')
assert.equal(response.text, '<xml><node>This is some content</node></xml>')
done()
})
}, 1200);
});
});
});

0 comments on commit 2e60c7b

Please sign in to comment.