Merge pull request #794 from sebgie/issue#570

Add invalidate cache headers
This commit is contained in:
Hannah Wolfe 2013-09-26 15:17:24 -07:00
commit 57d83fe560
2 changed files with 42 additions and 1 deletions

View File

@ -81,7 +81,14 @@ posts = {
} }
return canThis(this.user).remove.post(args.id).then(function () { return canThis(this.user).remove.post(args.id).then(function () {
return dataProvider.Post.destroy(args.id); return when(posts.read({id : args.id})).then(function (result) {
return dataProvider.Post.destroy(args.id).then(function () {
var deletedObj = {};
deletedObj.id = result.attributes.id;
deletedObj.slug = result.attributes.slug;
return deletedObj;
});
});
}, function () { }, function () {
return when.reject("You do not have permission to remove posts."); return when.reject("You do not have permission to remove posts.");
}); });
@ -301,6 +308,33 @@ settings = {
// ## Request Handlers // ## Request Handlers
function invalidateCache(req, res, result) {
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
method = req.method,
endpoint = parsedUrl[3],
id = parsedUrl[4],
cacheInvalidate;
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
if (endpoint === 'settings' || endpoint === 'users') {
cacheInvalidate = "/*";
} else if (endpoint === 'posts') {
cacheInvalidate = "/, /page/*, /rss/, /rss/*";
if (id) {
if (result.toJSON) {
cacheInvalidate += ', /' + result.toJSON().slug;
} else {
cacheInvalidate += ', /' + result.slug;
}
}
}
if (cacheInvalidate) {
res.set({
"X-Cache-Invalidate": cacheInvalidate
});
}
}
}
// ### requestHandler // ### requestHandler
// decorator for api functions which are called via an HTTP request // decorator for api functions which are called via an HTTP request
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response // takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
@ -312,6 +346,7 @@ requestHandler = function (apiMethod) {
}; };
return apiMethod.call(apiContext, options).then(function (result) { return apiMethod.call(apiContext, options).then(function (result) {
invalidateCache(req, res, result);
res.json(result || {}); res.json(result || {});
}, function (error) { }, function (error) {
error = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')}; error = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};

View File

@ -369,6 +369,9 @@ adminControllers = {
return api.notifications.add(notification).then(function () { return api.notifications.add(notification).then(function () {
delete req.session.user; delete req.session.user;
res.set({
"X-Cache-Invalidate": "/*"
});
res.redirect('/ghost/signin/'); res.redirect('/ghost/signin/');
}); });
@ -399,6 +402,9 @@ adminControllers = {
return api.notifications.add(notification).then(function () { return api.notifications.add(notification).then(function () {
delete req.session.user; delete req.session.user;
res.set({
"X-Cache-Invalidate": "/*"
});
res.redirect('/ghost/signup/'); res.redirect('/ghost/signup/');
}); });
}, function resetFailure(error) { }, function resetFailure(error) {