Enable HTTP Caching on resources.

This patch enables request caching for specifically addressed
resources. In order to avoid cache expiry, POST/PUT/DELETE requests
trigger a immediate cache update. Furthermore, angular-cache was
added to handle our maxAge expiration, with an expiration time
of one minute. This short time is intended to provide a reasonable
lifespan for each resource - one that both prevents
frequent reloads, but also doesn' tallow the resource to age too
much.

Change-Id: Ibbcb48c20fa99eb287c6543e042268ac8e842e17
This commit is contained in:
Michael Krotscheck 2014-11-20 14:12:49 -08:00
parent a9bc6c0478
commit 0274d2365d
5 changed files with 83 additions and 4 deletions

View File

@ -11,7 +11,8 @@
"angular-bootstrap": "0.11.2",
"angular-local-storage": "0.0.7",
"angular-elastic": "2.4.0",
"angular-moment": "0.8.2"
"angular-moment": "0.8.2",
"angular-cache": "3.2.2"
},
"devDependencies": {
"angular-mocks": "1.2.25",

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* An HTTP request interceptor that pays attention to POST/PUT/DELETE operations
* on specific resources, and replaces the local cached data with the resulting
* value.
*/
angular.module('sb.services').factory('httpCacheHandler',
function ($q, $cacheFactory) {
'use strict';
var $httpDefaultCache = $cacheFactory.get('$http');
return {
/**
* Handle a success response.
*/
response: function (response) {
var method = response.config.method;
var url = response.config.url;
var obj = response.data;
// Ignore GET methods.
switch (method) {
case 'POST':
if (obj.hasOwnProperty('id')) {
$httpDefaultCache.put(url + '/' + obj.id, obj);
}
break;
case 'PUT':
$httpDefaultCache.put(url, obj);
break;
case 'DELETE':
$httpDefaultCache.remove(url);
break;
default:
break;
}
return response;
}
};
})
// Attach the HTTP interceptor.
.config(function ($httpProvider) {
'use strict';
$httpProvider.interceptors.push('httpCacheHandler');
});

View File

@ -50,7 +50,7 @@ angular.module('sb.services')
},
'get': {
method: 'GET',
cache: false
cache: true
},
'update': {
method: 'PUT'
@ -62,6 +62,7 @@ angular.module('sb.services')
method: 'GET',
isArray: true,
responseType: 'json',
cache: false,
params: {
limit: getLimit
}
@ -71,6 +72,7 @@ angular.module('sb.services')
url: searchUrl,
isArray: true,
responseType: 'json',
cache: false,
params: {
limit: getLimit
}

View File

@ -26,7 +26,8 @@ angular.module('storyboard',
[ 'sb.services', 'sb.templates', 'sb.dashboard', 'sb.pages', 'sb.projects',
'sb.auth', 'sb.story', 'sb.profile', 'sb.notification', 'sb.search',
'sb.admin', 'sb.subscription', 'sb.project_group', 'ui.router',
'ui.bootstrap', 'monospaced.elastic', 'angularMoment'])
'ui.bootstrap', 'monospaced.elastic', 'angularMoment',
'angular-data.DSCacheFactory'])
.constant('angularMomentConfig', {
preprocess: 'utc',
timezone: 'UTC'
@ -57,4 +58,16 @@ angular.module('storyboard',
function () {
$state.go('index');
});
});
})
.run(function ($http, DSCacheFactory) {
'use strict';
DSCacheFactory.createCache('defaultCache', {
// Items added to this cache expire after 1 minute.
maxAge: 60000,
// Items will be deleted from this cache only on check.
deleteOnExpire: 'passive'
});
$http.defaults.cache = DSCacheFactory.get('defaultCache');
});

View File

@ -35,6 +35,7 @@
<script src="angular-resource/angular-resource.js"></script>
<script src="angular-local-storage/angular-local-storage.js"></script>
<script src="angular-sanitize/angular-sanitize.js"></script>
<script src="angular-cache/dist/angular-cache.js"></script>
<script src="moment/moment.js"></script>
<script src="angular-moment/angular-moment.js"></script>
<!-- endbuild -->