Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class RedisCache {

let request = response && response.req;
let key = this.cacheKey(path, request);
let documentIdMatch = /\/([\d]+)/.exec(path);
let documentId = documentIdMatch && documentIdMatch.length ? documentIdMatch[1] : null;

return new Promise((res, rej) => {
let statusCode = response && response.statusCode;
Expand All @@ -65,7 +67,7 @@ class RedisCache {

this.client.multi()
.set(key, body)
.set(`key_index_${path}`, key)
.set(`key_index_${documentId || path}`, key)
.expire(key, this.expiration)
.exec(err => {
if (err) {
Expand Down
16 changes: 16 additions & 0 deletions test/caching-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe('caching tests', function() {
});
});

it('can build a custom cache key from the documentId prefix', function() {
let body = '<body>Hola</body>';
let mockResponse = {
req: {
cookies: {
chocolateChip: 'mmmmmm'
}
}
};

return cache.put('/123-abc', body, mockResponse).then(() => {
expect(mockRedis['/123-abc_mmmmmm']).to.equal(body);
expect(mockRedis.key_index_123).to.equal('/123-abc_mmmmmm');
});
});

it('can get a cache item based on a custom cache key', function() {
let body = '<body>Hola</body>';
let cookieValue = 'mmmmmm';
Expand Down