Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure to expire the same key that was added #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 37 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const redis = require('redis');

const FIVE_MINUTES = 5 * 60;
const ONE_HOUR = 12 * FIVE_MINUTES;

class RedisCache {
constructor(options) {
Expand Down Expand Up @@ -52,27 +53,55 @@ 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;
let statusCodeStr = statusCode && (statusCode + '');
let keyIndex = `key_index_${documentId || path}`;

if (statusCodeStr && statusCodeStr.length &&
(statusCodeStr.charAt(0) === '4' || statusCodeStr.charAt(0) === '5')) {
res();
return;
}

this.client.multi()
.set(key, body)
.expire(path, this.expiration)
.exec(err => {
if (err) {
rej(err);
this.client.get(keyIndex, (err, reply) => {
if (err) {
rej(err);
} else {
let keys;
if (!reply) {
keys = [];
} else {
res();
try {
keys = JSON.parse(reply);
} catch (e) {
console.error(`can't parse key index for ${keyIndex}: ${reply}`);
keys = [ reply ];
}
}
});

keys.push(key);

console.log(`Creating cache key ${key}`);
console.log(`Setting cache key-index ${keyIndex}: ${JSON.stringify(keys)}`);

this.client.multi()
.set(key, body)
.set(keyIndex, JSON.stringify(keys))
.expire(key, this.expiration)
.expire(keyIndex, ONE_HOUR)
.exec(err => {
if (err) {
rej(err);
} else {
res();
}
});
}
});
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastboot-redis-cache",
"version": "0.1.0",
"version": "0.1.2",
"description": "A FastBoot App Server cache for Redis",
"main": "index.js",
"scripts": {
Expand Down
84 changes: 81 additions & 3 deletions test/caching-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('caching tests', function() {
beforeEach(function() {
cache = new RedisCache({
cacheKey (path, request) {
return `${path}_${request && request.cookies && request.cookies.chocolateChip}`;
return `${request.hostname}${path}_${request && request.cookies && request.cookies.chocolateChip}`;
}
});
cache.client = mockRedisClient();
Expand All @@ -78,22 +78,100 @@ describe('caching tests', function() {
let body = '<body>Hola</body>';
let mockResponse = {
req: {
hostname: 'foo.com',
cookies: {
chocolateChip: 'mmmmmm'
}
}
};

return cache.put('/', body, mockResponse).then(() => {
expect(mockRedis['/_mmmmmm']).to.equal(body);
expect(mockRedis['foo.com/_mmmmmm']).to.equal(body);
expect(mockRedis['key_index_/']).to.equal(JSON.stringify(['foo.com/_mmmmmm']));
});
});

it('can represent mulitple cache keys from different domains with the same path', function() {
let body = '<body>Hola</body>';
let body2 = '<body>Bonjour</body>';
let mockResponse = {
req: {
hostname: 'foo.com',
cookies: {
chocolateChip: 'mmmmmm'
}
}
};
let mockResponse2 = {
req: {
hostname: 'bar.com',
cookies: {
chocolateChip: 'mmmmmm'
}
}
};

return cache.put('/', body, mockResponse)
.then(() => {
expect(mockRedis['foo.com/_mmmmmm']).to.equal(body);
expect(mockRedis['key_index_/']).to.equal(JSON.stringify(['foo.com/_mmmmmm']));
})
.then(() => cache.put('/', body2, mockResponse2))
.then(() => {
expect(mockRedis['foo.com/_mmmmmm']).to.equal(body);
expect(mockRedis['bar.com/_mmmmmm']).to.equal(body2);
expect(mockRedis['key_index_/']).to.equal(JSON.stringify([
'foo.com/_mmmmmm',
'bar.com/_mmmmmm'
]));
});
});

it('can handle non-JSON keys (migration test)', function() {
mockRedis[`key_index_/`] = 'garbage_key';
let body = '<body>Hola</body>';
let mockResponse = {
req: {
hostname: 'foo.com',
cookies: {
chocolateChip: 'mmmmmm'
}
}
};

return cache.put('/', body, mockResponse)
.then(() => {
expect(mockRedis['foo.com/_mmmmmm']).to.equal(body);
expect(mockRedis['key_index_/']).to.equal(JSON.stringify([
'garbage_key',
'foo.com/_mmmmmm'
]));
});
});

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

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

it('can get a cache item based on a custom cache key', function() {
let body = '<body>Hola</body>';
let cookieValue = 'mmmmmm';
mockRedis[`/_${cookieValue}`] = body;
mockRedis[`foo.com/_${cookieValue}`] = body;
let mockRequest = {
hostname: 'foo.com',
cookies: {
chocolateChip: cookieValue
}
Expand Down