Skip to content

Commit

Permalink
Include resource query part in resource cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
joanniclaborde committed Apr 13, 2015
1 parent 503228e commit 8d7005e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
6 changes: 2 additions & 4 deletions lib/cached_resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ var CachedResource = function(options, auth, params, logger) {
CachedResource.prototype.get = function(callback) {
if (!this.resource.url) return callback(null, {});

// TODO: include the auth in the cache key
var resource_response = cache.get(this.resource.url);
var resource_response = cache.get(this.resource.key());
if (resource_response) {
this.logger.log('Resource recovered from cache: ' + this.resource.url, 3);
refreshCache.call(this, resource_response);
Expand Down Expand Up @@ -62,8 +61,7 @@ var getAndCache = function(callback) {
// The response has no cache headers, cache for a default duration
resource_response.expires_at = new Date().getTime() + DEFAULT_RESOURCE_FRESHNESS;
}
// TODO: include the auth in the cache key
cache.set(self.resource.url, resource_response);
cache.set(self.resource.key(), resource_response);
}

callback(err, resource_response);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"raven": "^0.6.2",
"socket.io": "~1.0.6",
"continuation-local-storage": "~3.1.1",
"solidus-client": "^1.1.0",
"solidus-client": "^1.2.0",
"browserify-transform-tools": "~1.2.1"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions test/fixtures/site 1/views/caching.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"title": "caching",
"resources": {
"cache1": {
"url": "https://solid.us/cache/1",
"url": "https://solid.us/cache",
"query": {"a": 1},
"optional": true
},
"cache2": "https://solid.us/cache/2"
"cache2": {
"url": "https://solid.us/cache",
"query": {"a": 2}
}
}
}
}}
Expand Down
38 changes: 19 additions & 19 deletions test/solidus.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,12 @@ describe( 'Solidus', function(){
request(solidus_server.router).get('/caching.json').end(function(err, res) {
if (err) throw err;
if (cache1) {
assert.equal(cache1, res.body.resources.cache1.test);
assert.equal(res.body.resources.cache1.test, cache1);
} else {
assert(!res.body.resources.cache1);
}
if (cache2) {
assert.equal(cache2, res.body.resources.cache2.test);
assert.equal(res.body.resources.cache2.test, cache2);
} else {
assert(!res.body.resources.cache2);
}
Expand All @@ -635,13 +635,13 @@ describe( 'Solidus', function(){
}

beforeEach(function() {
nock('https://solid.us').get('/cache/2').reply( 200, { test: 2 } );
nock('https://solid.us').get('/cache?a=2').reply( 200, { test: 2 } );
});

it( 'Caches the resources', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 1 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 1 } );
test_caching(1, 2, cb);
},
function(cb) {
Expand All @@ -653,11 +653,11 @@ describe( 'Solidus', function(){
it( 'Does not cache resources with invalid status codes', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 500, { test: 1 } );
nock('https://solid.us').get('/cache?a=1').reply( 500, { test: 1 } );
test_caching(null, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 3 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 3 } );
test_caching(3, 2, cb);
}
], done);
Expand All @@ -666,11 +666,11 @@ describe( 'Solidus', function(){
it( 'Does not cache resources with invalid data', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, 'not json' );
nock('https://solid.us').get('/cache?a=1').reply( 200, 'not json' );
test_caching(null, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 3 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 3 } );
test_caching(3, 2, cb);
}
], done);
Expand All @@ -679,11 +679,11 @@ describe( 'Solidus', function(){
it( 'Renders expired cached resources before refreshing them', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
test_caching(1, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 3 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 3 } );
test_caching(1, 2, cb);
},
function(cb) {
Expand All @@ -695,12 +695,12 @@ describe( 'Solidus', function(){
it( 'Locks expired cached resources while being refreshed', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
test_caching(1, 2, cb);
},
function(cb1) {
// Delay the response, to make sure the next request comes in before the first one is refreshed
nock('https://solid.us').get('/cache/1').delay(25).reply( 200, { test: 3 }, { 'Cache-Control': 'max-age=0' } );
nock('https://solid.us').get('/cache?a=1').delay(25).reply( 200, { test: 3 }, { 'Cache-Control': 'max-age=0' } );
async.parallel([
function(cb2) {
test_caching(1, 2, cb2);
Expand All @@ -713,7 +713,7 @@ describe( 'Solidus', function(){
function(cb) {
// The previous requests are done, but the refresh might not, wait to make sure the lock is released
setTimeout(function() {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 4 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 4 } );
test_caching(3, 2, cb);
}, 50);
},
Expand All @@ -726,15 +726,15 @@ describe( 'Solidus', function(){
it( 'Unlocks expired cached resources with invalid status codes', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
test_caching(1, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 500, { test: 3 } );
nock('https://solid.us').get('/cache?a=1').reply( 500, { test: 3 } );
test_caching(1, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 4 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 4 } );
test_caching(1, 2, cb);
},
function(cb) {
Expand All @@ -746,15 +746,15 @@ describe( 'Solidus', function(){
it( 'Unlocks expired cached resources with invalid data', function( done ){
async.series([
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 1 }, { 'Cache-Control': 'max-age=0' } );
test_caching(1, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, 'not json' );
nock('https://solid.us').get('/cache?a=1').reply( 200, 'not json' );
test_caching(1, 2, cb);
},
function(cb) {
nock('https://solid.us').get('/cache/1').reply( 200, { test: 4 } );
nock('https://solid.us').get('/cache?a=1').reply( 200, { test: 4 } );
test_caching(1, 2, cb);
},
function(cb) {
Expand Down

0 comments on commit 8d7005e

Please sign in to comment.